Java Exception Hierarchy detailed

Source: Internet
Author: User
Tags class definition throw exception throwable

Transferred from:

1. Java Exception Hierarchy

Exception refers to the various conditions, such as: file cannot be found, network connection failure, illegal parameters and so on. An exception is an event that interferes with the normal instruction flow during the program's run. Java describes various kinds of exceptions through the many subclasses of the Throwable class in the API. Thus, Java exceptions are objects, examples of throwable subclasses, and describe error conditions that occur in a piece of code. An error throws an exception when the condition is generated.

Java Exception class hierarchy diagram:

Figure 1 Java Exception class hierarchy diagram

In Java, all exceptions have a common ancestor throwable (can be thrown). Throwable specifies the commonality of any problem that can be transmitted through a Java application using the exception propagation mechanism in the code.

Throwable: There are two important subclasses: Exception (Exception) and error (Errors), both of which are important subclasses of Java exception handling, each of which contains a large number of subclasses.

Error (Error): A bug that the program cannot handle, indicating a more serious problem in running the application. Most errors are unrelated to what the code writer does, and represent a problem with the JVM (the Java virtual machine) that is running the code. For example, the Java Virtual machine runs the error (virtual Machineerror) and OutOfMemoryError occurs when the JVM no longer has the memory resources required to continue the operation. When these exceptions occur, the Java Virtual machine (JVM) typically chooses to terminate the thread.

These errors indicate that the failure occurred on the virtual machine itself, or when the virtual machine attempted to execute the application, such as a Java Virtual machine run error (virtual Machineerror), a class definition error (NOCLASSDEFFOUNDERROR), and so on. These errors are not available because they are outside the control and processing power of the application and are mostly not allowed when the program is running. For a well-designed application, even if it does, it should not be an attempt to deal with the anomalies that it causes. In Java, errors are described by the subclasses of the error.

Exception (Exception): is an exception that the program itself can handle.

The Exception class has an important subclass runtimeexception. The RuntimeException class and its subclasses represent errors thrown by the JVM common operation. For example, if you attempt to use null object references, divide by zero, or array out of bounds, the run-time exceptions (NullPointerException, ArithmeticException), and arrayindexoutofboundexception are raised respectively.

Note: Exceptions and errors are different: Exceptions can be handled by the program itself, and errors cannot be handled.

In general, Java exceptions (including exception and error) are categorized as checked exceptions and non-checked exceptions (unchecked exceptions).

can check exceptions (compiler requirements must be disposed of exceptions): The correct program in operation, it is easy to appear, reasonable tolerance of abnormal conditions. Although the abnormal condition can be checked, it can be predicted to some extent, and in the event of such abnormal situation, it must be handled in some way.

In addition to RuntimeException and its subclasses, other exception classes and their subclasses belong to the exception-checking. This exception is characterized by the Java compiler checking it, that is, when such an exception can occur in a program, either by capturing it with a try-catch statement or by declaring it with a throws clause, or the compilation will not pass.

exception not checked (compiler does not require forced disposition of exceptions): Includes run-time exceptions (RuntimeException with its subclasses) and errors (error).

Exception This exception is divided into two major classes of runtime exceptions and non-runtime exceptions (compilation exceptions). These exceptions should be handled as much as possible in the program.

Run-time Exceptions: both the RuntimeException class and its subclass exceptions, such as nullpointerexception (null pointer exception), indexoutofboundsexception (subscript out-of-bounds exception), etc. These exceptions are not checked for exceptions, the program can choose to capture the processing, or it can not be processed. These exceptions are usually caused by a program logic error, and the program should avoid this kind of exception as much as possible from a logical point of view.

A run-time exception is characterized by the Java compiler not checking it, that is, when such an exception can occur in a program, even if it is not captured with the Try-catch statement, it is not thrown with the throws clause declaration, and it is compiled.

Non-runtime exception (compile exception): is an exception other than RuntimeException, and the type belongs to the exception class and its subclasses. From the point of view of the program syntax is the exception that must be handled, and if not handled, the program cannot be compiled through. such as IOException, SqlException, and user-defined exception exceptions, generally do not customize check exceptions.

2. Handling Exception Mechanisms

In a Java application, the exception handling mechanism is: Throw an exception, catch an exception.

Throw exception : When a method throws an exception with an error, the method creates the exception object and delivers the runtime system, which contains exception information such as the type of exception and the state of the program when the exception occurred. The runtime system is responsible for finding and executing the code that handles the exception.

catch Exception : After the method throws an exception, the runtime system will look for the appropriate exception handler (exception handler). A potential exception handler is a collection of methods that persist in the call stack, in turn, when an exception occurs. An appropriate exception handler is the exception type that can be handled by the exceptions handler when it matches the type of exception thrown by the method. The runtime system starts with the method in which the exception occurred and then turns back to the method in the call stack until it finds the method that contains the appropriate exception handler and executes it. The runtime system terminates when the runtime system traverses the call stack without finding an appropriate exception handler. At the same time, it means the termination of the Java program.

Java technology requires different exception handling for run-time exceptions, errors, or exceptions that can be checked.

Because of the non-availability of runtime exceptions, in order to make the application more reasonable and easier to implement, Java rules that runtime exceptions are automatically thrown by the Java runtime system, allowing applications to ignore runtime exceptions.

Java allows the method to not make any throw declarations when the run method does not want to catch the error that may occur in the method's run. Because most error exceptions belong to a condition that can never be allowed to occur, it is also a reasonable exception that the application should not catch.

For all the exceptions that can be checked, the Java rule is that a method must catch, or declare the throw method. That is, when a method chooses not to catch an exception, it must declare that it will throw an exception.

The ability to catch exceptions requires a matching type of exception handler. The exception that is caught may be an exception that is thrown by an individual statement, or an exception that is thrown by a method called or by the Java Runtime System. In other words, the exception that a method can catch must be the exception that Java code throws on the premises. Simply put, the exception is always first thrown and then captured.

Any Java code can throw exceptions, such as code written by itself, code from the Java Development environment package, or a Java runtime system. No matter who, you can throw an exception through the Java throw statement.

Any exceptions that are thrown from a method must use the throws clause.

Catch exceptions are implemented through TRY-CATCH statements or try-catch-finally statements.

Overall, Java rules: You must catch, or declare, the exception to be checked. Allow RuntimeException and error to be ignored.

(GO) Java exception hierarchy detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.