[Weave message Frame] [Java Core technology] Exception basics

Source: Internet
Author: User
Tags stack trace throwable

Java Exception Architecture

Thorwable class for all exceptions and errors, there are two subclasses of error and exception, respectively, representing errors and exceptions.
Where the exception class Exception is divided into runtime exceptions (runtimeexception) and compile-time exceptions (checked Exception),
The differences and linkages between these exceptions are described in detail below:

1.Error and exception

Error is a bug that the program cannot handle, such as OutOfMemoryError, Threaddeath, and so on. When these exceptions occur, the Java Virtual machine (JVM) typically chooses to terminate the thread.

Exception is an exception that the program itself can handle, which is divided into two classes of runtime exceptions and non-runtime exceptions. these exceptions should be handled as much as possible in the program.

2. Run-time exceptions and compile-time exceptions

Run-time exceptions are runtimeexception classes and their subclass exceptions, such as NullPointerException, Indexoutofboundsexception, and so on.
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.

The compile-time exception is an exception outside of 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.

3. Abnormal chain

Designed to clearly know the program error call process, that is, transitivity, generally only care about the first and last exception information

SOURCE Analysis

Divided into three parts:

1. Construction Exceptions

2. Register the current exception to the exception list depth coordinates

3. Print the exception chain

Part I: A top-level class Throwable constructed method is called each time the exception class is constructed

     Public Exception (String message) {        Super(message);    }       Public Exception (String message, throwable cause) {        Super(message, cause);    }
   
Public throwable (String message) { fillinstacktrace (); = message; } Public throwable (String message, throwable cause) { fillinstacktrace (); = message; this. Cause = cause; }

Part Two: Registering to the exception list

     Public synchronizedthrowable fillinstacktrace () {if(StackTrace! =NULL||BackTrace!=NULL /*Out of protocol State*/ ) {        //Insert exception list 0 for inserting to first, that is, the last exception is put to the headFillinstacktrace (0); StackTrace=Unassigned_stack; }        return  This; }    Private nativeThrowable Fillinstacktrace (intdummy);

Part III: Print the exception chain, because the last exception is registered to the head, so the loop output order is from the end of the tail

1      Public voidPrintstacktrace () {2 Printstacktrace (system.err);3     }4 5      Public voidPrintstacktrace (PrintStream s) {6Printstacktrace (NewWrappedprintstream (s));7     }8 9     Private voidPrintstacktrace (Printstreamorwriter s) {Ten         //Guard against malicious overrides of Throwable.equals by One         //using a Set with the identity equality semantics. ASet<throwable> Dejavu = -Collections.newsetfrommap (NewIdentityhashmap<throwable, boolean>()); -Dejavu.add ( This); the  -         synchronized(S.lock ()) { -             //Print our stack trace -S.println ( This); +Stacktraceelement[] Trace =getourstacktrace (); -              for(stacktraceelement traceelement:trace) +S.println ("\tat" +traceelement); A  at             //Print suppressed exceptions, if any -              for(Throwable se:getsuppressed ()) -Se.printenclosedstacktrace (S, Trace, suppressed_caption, "\ T"), Dejavu); -  -             //Print Cause, if any -Throwable Ourcause =getcause (); in             if(Ourcause! =NULL) -Ourcause.printenclosedstacktrace (S, Trace, cause_caption, "", Dejavu); to         } +     } -      the      Publicstacktraceelement[] Getstacktrace () { *         returnGetourstacktrace (). Clone (); $     }Panax Notoginseng  -     Private synchronizedstacktraceelement[] Getourstacktrace () { the         //Initialize stack trace field with information from +         //BackTrace If This is the first call to this method A         if(StackTrace = = Unassigned_stack | | the(StackTrace = =NULL&& BackTrace! =NULL)/*Out of protocol State*/) { +             intdepth =getstacktracedepth (); -StackTrace =Newstacktraceelement[depth]; $              for(inti=0; I < depth; i++) $Stacktrace[i] =getstacktraceelement (i); -}Else if(StackTrace = =NULL) { -             returnUnassigned_stack; the         } -         returnStackTrace;Wuyi}
Print Exception Chain

4. Tracking Error Codes

The first output is the last exception: That means you can trace the outermost error method

The last output is the first exception: that is, tracing to the source of the error

Here's a sample description:

1  PackageCom.eyu.onequeue;2 3  Public classTestException {4      Public Static voidMain (string[] args) {5         Newtestexception (). Demo ();6     }7 8      Public voidA () {9         Try {Ten b (); One}Catch(Exception e) { A             Throw NewRuntimeException ("Call a", e); -         } -  the     } -  -      Public voidB () { -         Try { + c (); -}Catch(Exception e) { +             Throw NewRuntimeException ("Call B", e); A         } at     } -  -      Public voidC () { -         Throw NewRuntimeException ("Call C"); -     } -  in      Public voidDemo () { - a (); to     } +}

Exception in thread "main" Java.lang.RuntimeException:call a
At Com.eyu.onequeue.testexception.a (testexception.java:12)
At Com.eyu.onequeue.TestException.demo (testexception.java:30)
At Com.eyu.onequeue.TestException.main (testexception.java:5)
caused By:java.lang.RuntimeException:call b
At com.eyu.onequeue.testexception.b (testexception.java:21)
At Com.eyu.onequeue.testexception.a (testexception.java:10)
... 2 more
caused By:java.lang.RuntimeException:call C
At COM.EYU.ONEQUEUE.TESTEXCEPTION.C (testexception.java:26)
At com.eyu.onequeue.testexception.b (testexception.java:19)
... 3 more

The C method is the source of the error, and a method is the caller

Tip: When the exception information is too high, the filter condition can quickly locate the source of the error with the project package name or class name, which is usually an error in the application logic.

The following section exploits the exception development project

Finally, the common JDK exceptions are given.

[Weave message Frame] [Java Core technology] Exception basics

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.