Java.lang.Throwable anomaly/in-depth

Source: Internet
Author: User
Tags terminates

There are several phenomena that need to be summed up:
--------------------------------------
In the Java language, the base class of the error class is Java.lang.Error, and the base class of the exception class is java.lang.Exception.

The same points: both Java.lang.Error and java.lang.Exception are subclasses of the java.lang.Throwable, 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.

Differences: Java.lang.Error itself and its subclasses do not require support for the Try-catch statement, which can be returned at any time, as defined by the following method:

public thrownew 

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 wrong :

public thrownew 

The correct method is defined as follows:

 Public throws  throwNew

Where the MyException class is a subclass of Java.lang.Exception.

----------------------------------------

1) If there is a place in the code where an exception occurred, that is, Throwable is new out of the place, because the Throwable constructor directly called the Fillinstacktrace (), then the Throwable will step-by-step tracking method calls, Until the end of the thread is traced, for example, the main thread of the main () method, the other thread of the run () method.
2) Fillinstacktrace is the native method, and only the native method can complete such code tracing.
3) Throwable has a method outside of Setstacktrace () (Note that setstacktrace is just use by RPC frameworks and other advanced systems), That is, setstacktrace () is usually not used by applications.
4) In contrast to Setstacktrace (), the information in the StackTrace can be obtained by invoking the Fillinstacktrace () method, which can also be obtained by means of a deserialize method; you can see its writeobject () Method.
5) The Fillinstacktrace () method returns the Throwable as the this pointer; This makes it easy to use the throw ex.fillinstacktrace ();

--------------------------------------------

An exception is an anomaly that occurs when the JVM (the Java Virtual machine) encounters a method of executing an application, and the JVM generates an exception object that is thrown to the customer for exception handling. The exception handling mechanism of the struts framework is based on Java exception handling. Before studying struts exception handling, let's review the Java exception handling principle, Understanding the Java Virtual Machine JVM's exception handling process helps to apply design-proper exception handling methods. Handling exceptions requires the JVM to pay a lot of overhead, so it must be used with caution.

Java exceptions are objects that are created when a Java program runs, and it encapsulates exception information, and the root class of Java exceptions is java.lang.Throwable. The entire class has two direct subclasses Java.lang.Error and Java.lang.Exception.Error are critical errors that the program itself cannot recover. The Exception represents an exception error that can be caught and handled by the program. The JVM uses the method call stack to track a series of method call procedures in each thread , the stack holds local information for each method that is called. For standalone Java programs, you can always go to the main method of the program. When a new method is called, the JVM puts the stack structure that describes the method on top of the stack, and the method at the top is the correct one. When a Java method executes properly, The JVM returns to the stack structure of the method from the call stack and proceeds to the previous method. If the Java method throws an exception while executing the code, the JVM must find the catch block code that catches the exception. It first looks at the current method whether the thief is such a catch code block, Executes the catch code block if it exists, or the JVM returns to the stack structure of the method at the call stack and proceeds to find the appropriate catch block in the previous method. Finally, if the JVM catches up to the main () method, it always throws the exception to the main () method, The exception-handling code block is still not found, and the thread terminates abnormally, and if the thread is the mainline, the application terminates, and the JVM throws the exception directly to the user and sees the original exception information on the user terminal.

This article transferred from: http://nakata-yf.iteye.com/blog/23569

Java.lang.Throwable anomaly/in-depth

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.