What is the relationship between Error and Exception

Error and Exception both inherit from Throwable. The inheritance relationship is shown in the figure below:

Error and Exception are two major classes of Throwable design in the Java language: Errors are usually serious and should not be caught, such as OutOfMemoryError and StackOverFlowError. Exception is something that programmers can intervene in and catch. Take a look at the comments for the Java source code.

1. Error

/**
 * An {@code Error} is a subclass of {@code Throwable}
 * that indicates serious problems that a reasonable application
 * should not try to catch. Most such errors are abnormal conditions.
 * The {@code ThreadDeath} error, though a "normal" condition,
 * is also a subclass of {@code Error} because most applications
 * should not try to catch it.
 * <p>
 * A method is not required to declare in its {@code throws}
 * clause any subclasses of {@code Error} that might be thrown
 * during the execution of the method but not caught, since these
 * errors are abnormal conditions that should never occur.
 *
 * That is, {@code Error} and its subclasses are regarded as unchecked
 * exceptions for the purposes of compile-time checking of exceptions.
 *
 * @author  Frank Yellin
 * @see     java.lang.ThreadDeath
 * @jls 11.2 Compile-Time Checking of Exceptions
 * @since   1.0
 */
public class Error extends Throwable
Copy the code

Error is a serious problem and should not be caught; It is an unchecked Exception, that is, you don’t need to declare throws on methods, and it is not a checked exception that the compiler can find.

2. Exception

/**
 * The class {@code Exception} and its subclasses are a form of
 * {@code Throwable} that indicates conditions that a reasonable
 * application might want to catch.
 *
 * <p>The class {@code Exception} and any subclasses that are not also
 * subclasses of {@link RuntimeException} are <em>checked
 * exceptions</em>.  Checked exceptions need to be declared in a
 * method or constructor's {@code throws} clause if they can be thrown
 * by the execution of the method or constructor and propagate outside
 * the method or constructor boundary.
 *
 * @author  Frank Yellin
 * @see     java.lang.Error
 * @jls 11.2 Compile-Time Checking of Exceptions
 * @since   1.0
 */
public class Exception extends Throwable
Copy the code

Exceptions and their subclasses can be handled by a catch. Exception and its subclasses (excluding RuntimeException and its subclasses) are checked exceptions and can be detected by the compiler. That is, if you don’t show handling these Checked exceptions, the compilation won’t pass. How do you deal with that? You can declare throws on methods or constructors, or add try catch blocks.

Writing here, everything except checked Exception is unchecked exception. Checked refers to the compiler, only the compiler “checks”; Runtime exceptions and errors are obviously not “checked” by the compiler.

NoClassDefFoundError and ClassNotFoundException

What’s the difference between the two. One is Error and the other is Exception. Take a look at the Java definition:

1. ClassNotFoundException

/** * Thrown when an application tries to load in a class through its * string name using: * <ul> * <li>The <code>forName</code> method in class <code>Class</code>. * <li>The <code>findSystemClass</code> method in class * <code>ClassLoader</code> . * <li>The <code>loadClass</code> method in class <code>ClassLoader</code>. * </ul>  * <p> * but no definition for the class with the specified name could be found. * * * @author unascribed * @see java.lang.Class#forName(java.lang.String) * @see java.lang.ClassLoader#findSystemClass(java.lang.String) * @see java.lang.ClassLoader#loadClass(java.lang.String, Boolean) * @ since 1.0 * / public class ClassNotFoundException extends ReflectiveOperationExceptionCopy the code

As you can see, it is a reflection exception, not a RuntimeException. That is, it is a Checked exception. Throw or try catch must be displayed, otherwise the compiler will fail.

2. NoClassDefFoundError

/**
 * Thrown if the Java Virtual Machine or a <code>ClassLoader</code> instance
 * tries to load in the definition of a class (as part of a normal method call
 * or as part of creating a new instance using the <code>new</code> expression)
 * and no definition of the class could be found.
 * <p>
 * The searched-for class definition existed when the currently
 * executing class was compiled, but the definition can no longer be
 * found.
 *
 * @author  unascribed
 * @since   1.0
 */
public
class NoClassDefFoundError extends LinkageError
Copy the code

NoClassDefFoundError is a class whose definition is not found in the JVM when an instance of the class is new. That is, the class was present at compile time, was already loaded into the JVM, but was not present at the time of the new instance.

3. Reference links

  • Checked and unchecked exceptions in java with examples
  • After reading the Exception and Error, you will have no problem arguing with the interviewer
  • Difference between ClassNotFoundException and NoClassDefFoundError

Hard advertising

Welcome to the public account: Double6