Java BASICS (Exception/in-depth java. Lang. throwable)

Source: Internet
Author: User

Reprinted from: http://www.51edu.com/it/2008/1120/article_3272.html

 

There are several phenomena that need to be summarized:
In Java, the base class of the error class is Java. Lang. error, and the base class of the exception class is Java. Lang. Exception.
Similarities: Java. lang. error and Java. lang. all exceptions are Java. lang. throwable subclass, so Java. lang. error and Java. lang. exception itself and its subclasses can all be used as throw objects, such as throw new myerror (); and throw new myexception (). Among them, the myerror class is Java. lang. the subclass of error. The myexception class is Java. lang. the subclass of exception.
Differences: Java. Lang. Error itself and its subclasses do not require the support of the try-catch statement. You can return methods at any time, as defined in the following method:
Public String mymethod (){
Throw new myerror ();
}
The myerror class is a subclass of the Java. Lang. Error class.
Java. Lang. Exception itself and its subclass must be supported by the try-catch statement. The following method definition is incorrect:
Public String mymethod (){
Throw new myexception ();
}
The correct method is defined as follows:
Public String mymethod () throws myexception {
Throw new myexception ();
}
The myexception class is a subclass of Java. Lang. Exception.
1) if there is an exception in the code, that is, the place where throwable is new, take the test. it is prompted that because the throwable constructor directly calls fillinstacktrace (), throwable will trace the call of the method step by step until it traces the terminal of the thread, for example, the main () method of the main thread, and the run () method of other threads.
2) fillinstacktrace is the native method, and only the native method can trace such code.
3) throwable has a method other than setstacktrace () (note that setstacktrace is only used by RPC frameworks and other advanced systems), that is, setstacktrace () is generally not used by applications.
4) Contrary to setstacktrace (), information in stacktrace can be obtained by calling the fillinstacktrace () method, and can also be obtained through the deserialize method. You can look at its writeobject () method.
5) throwable returned by the fillinstacktrace () method is the this pointer, which makes it easy to use throw ex. fillinstacktrace ();
An exception occurs when the JVM (Java Virtual Machine) executes a method of an application. The JVM generates an exception object and sends it to the customer for exception handling. the exception handling mechanism of Struts Framework is based on Java exception handling. before studying struts Exception Handling, let's review the Java Exception Handling principles and understand the exception handling process of Java Virtual Machine JVM, which helps the application design the correct exception handling methods. handling exceptions requires a lot of JVM overhead, so it must be used with caution.
A Java exception is an object created when the Java program runs abnormally. It encapsulates the exception information. The Java exception root class is Java. lang. throwable, the entire class has two direct subclass Java. lang. error and Java. lang. exception. error is a serious error that cannot be recovered by the program itself. exception indicates the exception errors that can be captured and processed by the program. JVM uses the method call stack to track a series of method call processes in each thread. The stack stores the local information of each method call. for independent Java programs, you can continue to the main method of the program. when a new method is called, JVM places the stack structure describing the method into the stack top. The method at the stack top is the correct method for execution. when a Java method is successfully executed, the JVM replays the stack structure of the method from the call stack and continues to process the previous method. if the Java method throws an exception during code execution, the JVM must find the Catch Block Code that can catch the exception. it first checks whether the current method is a catch code block such as a thief. If the catch code block exists, it executes the catch code block. Otherwise, the JVM calls the stack structure of the method in the stack, continue to the previous method to find the appropriate catch code block. finally, if the JVM traces the main () method up, that is, the exception is always thrown to the main () method, and the code block for exception handling is still not found, the thread will terminate the exception, if the thread is the main thread and the application is terminated, the JVM will directly throw the exception to the user and the original exception information will be displayed on the user terminal.

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.