Java Exception structure

Source: Internet
Author: User
Tags finally block throwable

Java exception mechanism

An exception is an error that occurs while the program is running. This article mainly teaches the Java language exception processing. The exception processing framework of Java language is an important embodiment of the robustness of Java language.

Java treats exceptions as objects and defines a base class java.lang.Throwable as a superclass of all exceptions. Many exception classes have been defined in the Java API, and these exception classes fall into two main categories, error errors and exception exception. The Java exception architecture is tree-like, with a hierarchical diagram of the structure:

1, Thorwable class

Thorwable class for all exceptions and errors, there are two subclasses of error and exception, respectively, representing errors and exceptions. The exception class Exception is divided into run-time exceptions (RuntimeException) and non-runtime exceptions, the two exceptions are very different, also known as the non-check exception (unchecked Exception) and check the exception (Checked Exception).


2.Error (Error):

Java Virtual machine-related problems, such as system crashes, virtual machine error, dynamic link failure, etc., this error can not be recovered or impossible to capture, will lead to application disruption, usually the application cannot handle these errors, so the application should not catch the Error object, It is also not necessary to declare in its throws clause that the method throws any error or its subclasses.

3.Exception:

The exception class and its subclasses are a form of throwable that identifies the conditions that a reasonable application wants to capture

(1) SQLException: This exception provides information about database access errors or other errors.

(2) RuntimeException is a superclass of exceptions that may be thrown during the normal operation of a Java virtual machine

(3) IOException: This class is a generic class of exceptions that are generated by failed or interrupted I/O operations.


4. Run-time exceptions and non-runtime exceptions

(1) Runtime exception is RuntimeException class and its subclass exception, such as NullPointerException, indexoutofboundsexception, etc., these exceptions are not check exceptions, the program can choose to capture processing, can also 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.

(2) A non-runtime exception is an exception that is runtimeexception except that 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.

(3) Spring transaction defaults to rollback only for runtime exception runtimeexception

5. Abnormal capture and processing

Java exception capture and processing is a difficult thing to grasp, if handled improperly, not only will make the program code readability greatly reduced, but also caused the system performance is low, even caused some difficult to find fault.

Java exception handling involves five keywords, namely: try, catch, finally, throw, throws. Here is a brief introduction, through the understanding of these five keywords, master the basic exception processing knowledge.

(1), try: It puts code that might throw an exception

(2), catch: Corresponds to the exception type and a block of code that indicates that the catch block is used to handle this type of code block and can have multiple catch blocks.

(3), finally: mainly for recycling in the try block of material resources (such as database connections, network connections and disk files), the exception mechanism always ensures that the finally block is always executed. Only the finally block, after execution completes, will not come back to execute the return or throw statement in the try or catch block, if finally the statement of the terminating method such as return or throw is used, then the execution will not be jumped back and stopped directly.

(4), throw: used to throw an actual exception, can be used as a statement, throw a specific exception object.

(5), throws: Used in a method signature to declare an exception that the method might throw, and to throw multiple exception types.

6, the difference between the throw and throws keywords

(1) The Throw keyword is used inside the method body to throw an exception of type Throwable. If you throw a check exception, you should also declare the type of exception that the method might throw on the method header. The caller of the method must also check the exception that is thrown by the handle. If all of the methods are thrown at layers, the JVM will eventually be processed and the processing is simple, which is to print the exception message and stack information. If an error or runtimeexception is thrown, the caller of the method can choose to handle the exception.

(2) The throws keyword is used in the method declaration section outside the method body to declare that the method may throw some exceptions. Only if a check exception is thrown, the caller of the method must handle or re-throw the exception. When the caller of the method is unable to handle the exception, it should continue to throw, rather than swallowed generally print the stack information in the catch block and do a little bit of processing.

A simple example is given below:

the common runtimeexception

nullpointerexception-NULL pointer Reference exception
ClassCastException-type cast exception.
IllegalArgumentException-Pass an illegal parameter exception.
ArithmeticException-Arithmetic Operation exception
Arraystoreexception-Storing an incompatible object exception with the declaration type in the array
Indexoutofboundsexception-Subscript out-of-bounds exception
Negativearraysizeexception-Create an array error exception with a negative size
NumberFormatException-Numeric Format exception
SecurityException-Security exception
Unsupportedoperationexception-Unsupported Operation exception

Original: http://blog.csdn.net/qq_27093465/article/details/52268531

Java Exception structure

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: info-contact@alibabacloud.com 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.