Deep java.lang.Throwable

Source: Internet
Author: User

Deep java.lang.Throwable

In the Java language, the base class for the error class is Java.lang.Error, and the base class for the exception class is java.lang.Exception.

Same point: Java.lang.Error and Java.lang.Exception are java.lang.Throwable subclasses, so java.lang.Error and java.lang.Exception themselves and their subclasses can be used as throw Like: throw new Myerror (); and throw new MyException (); The Myerror class is a subclass of Java.lang.Error, and the MyException class is a subclass of Java.lang.Exception.

Different points: Java.lang.Error itself and its subclasses do not need the support of the Try-catch statement, you can return the method at any time, such as the following method definition:
Public String MyMethod () {
throw new Myerror ();
}
Where the Myerror class is a subclass of the Java.lang.Error class.

The java.lang.Exception itself and its subclasses require support for the Try-catch statement, as 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 ();
}
Where the MyException class is a java.lang.Exception subclass.

&NBSP
1) If there is a place in the code where an exception occurred, that is, the place where Throwable was created, because Fillinstacktrace () was called directly in the Throwable constructor,
Then Throwable will trace the invocation of the method step-by-step until it is traced to the thread's terminal, for example, main thread's main () method, and the other thread's run () method.
2) Fillinstacktrace is the native method, and only the native method can complete such code tracing.
3) Throwable has a method Setstacktrace () (Note that Setstacktrace is only used by RPC frameworks and other advanced systems),
That is, setstacktrace () is typically not used by applications.
4) In contrast to Setstacktrace (), the information in StackTrace can be obtained by invoking the Fillinstacktrace () method, in addition to the deserialize method;
Take a look at its writeobject () method.
5) The Throwable returned by the Fillinstacktrace () method is the this pointer, which makes it easy to use throw ex.fillinstacktrace (); The
 
exception is an anomaly that occurs when the JVM (Java Virtual machine) executes a method of an application, and the JVM generates an exception object that is thrown at the customer for exception handling.
The exception handling mechanism of the struts framework is based on Java exception handling. Before studying struts exception handling, review the Java exception handling principle to understand the Java Virtual machine JVM exception
The process helps to apply the correct method of exception handling. Handling exceptions requires the JVM to pay a lot of money, so it is used for caution.

A

Java exception is an object created when a Java program encounters an abnormal condition that encapsulates exception information, and the root class of the Java exception is java.lang.Throwable,
The entire class has two direct subclasses Java.lang.Error and java.lang.Exception.Error are serious errors that the program itself cannot recover. Exception represents an exception error that can be caught and handled by a thread
order. The JVM uses the method call stack to track a series of method invocation procedures in each thread, the stack holds the local information for each calling method. For a stand-alone Java program, the
can continue to the main method of the program. When a new method is invoked, the JVM puts the stack structure that describes the method on top of the stack, The method at the top of the stack is the correct execution method.
When a Java method completes normally, the JVM returns to the stack structure of the method from the call stack, and then continues with the previous method. If the Java method throws an exception during code execution,
The JVM must find a catch block code that catches the exception. It first looks at whether the current method is a thief such a catch code block, if it exists, executes the catch code block, or the JVM returns to the call stack
where the stack structure of the method. Continue to the previous method to find the appropriate catch code block. Finally, if the JVM catches up to the main () method, which is to throw the exception to the main () method, the
still does not find the code block for the exception-handling, and the thread terminates abnormally, if the thread is the main path, The application also terminates, at which point the JVM throws the exception directly to the user, and the
sees the original exception information on the user's 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.