Java exception learning Summary

Source: Internet
Author: User

1. Official API explanation: 

1. throwable: exception and error base class. It provides a series of methods such as error stack implementation. Two direct subclasses: Error & exception

2. Differences between the two subclasses:

Error:ProgramErrors that should not be captured should be handled by JVM. It may be a very important error.

Exception: errors to be captured in the program.

Runtimeexception: a runtime exception that is a subclass of exception, but does not need to be caught.

Ii. RespectiveCodeRepresentation in

Error & runtimeexception: If no exception is needed, that is, try/catch or throws is not required. The Compiler recognizes this method;

Exception (exclude runtimeexception): the compiler forces exception handling, that is, it must try/catch or throws;

Further explanation: exception is divided into two types: checked exception and unchecked exception,

Unchecked exception refers to the exceptions that do not need to be checked, that is, runtimeexception/error. The runtime error JVM is taken care;

Checked exception indicates an exception that must be handled; otherwise, the compiler will not let it go;

3. Why is this design?

As mentioned above, throwable has two types of dimensions: 1. Checked 2. Exception level

Checked or not: the appearance is obvious, and the compiler will forcibly detect it;

Exception level: errors and exceptions. How can we differentiate them? I am really confused. It should be my personal understanding.

Iv. Knowledge about common key points of exceptions

1. Retain the exception Source

Exception Handling is a hierarchical tracing mechanism. Therefore, an exception can be thrown multiple times, but the exception source can be retained without changing the handle of the source exception and Its stacktrace.

That is, the simple throw E (the original exception), the exception source is retained.

2. Change the exception Source

To change the exception source, throw a new exception or change its stacktrace in hierarchical processing.

For example, E. fillstacktrace () or throw new inititon ("...") in the call method can locate the current method as the exception source.

3. Custom exceptions

Generally, custom exceptions are mainly checked, that is, inheriting from exceptions. The implementation is very simple, and the complexity depends on the business needs.

4. attach some code and refer to the implementation in thinking in Java. 

 Public   Class Throwableexceptionstudy {  //  Exception Test      Public   Static   Void Main (string [] ARGs) Throws  Interruptedexception {rethrowtest ();  //  Exception re-throw test (controllable source change) Rethrownew (); //  Throw a new exception test (Change source) Runtimeexceptiontest (); //  Runtime exception Test Errortest (); //  Error Test  }  /**  * Re-Throw the exception for testing.  */      Private   Static   Void  Rethrowtest (){  Try  {G ();}  Catch (Exception e ){ //  Throw exception System. Out. println ("caught exception in rethrowtest, E. printstacktrace ()" ); E. printstacktrace ();}  Catch (Throwable t ){ //  Throw throw System. Out. println ("caught throwable in rethrowtest, T. printstacktrace ()" ); T. printstacktrace ();}}  /**  * Exception Source  */      Private   Static   Void F ()Throws  Exception {system. Out. println ( "Originating the exception in F ()" );  Throw   New Exception ("thrown from F ()" );}  /**  * Re-throw an exception without changing the exception handle * is thrown in two ways: 1. Do not change the exception source. 2. Change the exception source.  */      Private   Static   Void G () Throws Throwable {  Try  {F ();}  Catch  (Exception e) {system. Out. println ( "Inside g (), E. printstacktrace ()" ); E. printstacktrace ();  //  Throw E;  //  A: Do not change the exception Source              Throw E. fillinstacktrace (); //  B: Change the exception source through fillinstatcktrace. }}  /**  * Throws a new exception and changes the exception handle.  */      Private   Static   Void  Rethrownew (){  Try  {F ();}  Catch  (Exception e) {system. Out. println ( "Caught in rethrownew, E. printstacktrace ()" ); E. printstacktrace ();  // Throw new nullpointerexception ("from rethrownew ");  //  Runtimeexception, automatically handled by the compiler              Try  {  Throw   New Exception ("from rethrownew "); //  Non-runtimeexception, must be captured } Catch  (Exception E1) {e1.printstacktrace ();}}}  /**  * Exceptions during runtime are not caught and will be automatically handled by the compiler. */      Private   Static   Void  F1 (){  Throw   New Runtimeexception ("from F1 ()" );}  /**  * Exception tests during runtime  */      Private   Static   Void  Runtimeexceptiontest (){  Try {F1 ();}  Catch  (Runtimeexception e) {e. printstacktrace ();}}  /**  * Error  */      Private   Static   Void  Error (){  Throw   New Error ("custome error, not exception ." );}  /** * Error Test  */      Private   Static   Void  Errortest (){  Try  {Error ();}  Catch  (Error t) {T. printstacktrace ();}}} 

Running result:

 Originating the exception in F () inside g (), E. printstacktrace () Java. lang. exception: thrown from F () at com. chq. study. throwableexceptionstudy. F (throwableexceptionstudy. java: 40) At com. chq. Study. throwableexceptionstudy. G (throwableexceptionstudy. Java: 49 ) At com. chq. Study. throwableexceptionstudy. rethrowtest (throwableexceptionstudy. Java: 25 ) At com. chq. Study. throwableexceptionstudy. Main (throwableexceptionstudy. Java: 14 ) Caught exception in rethrowtest, E. printstacktrace () Java. Lang. Exception: thrown from F () at com. chq. Study. throwableexceptionstudy. G (throwableexceptionstudy. Java: 54 ) At com. chq. Study. throwableexceptionstudy. rethrowtest (throwableexceptionstudy. Java: 25 ) At com. chq. Study. throwableexceptionstudy. Main (throwableexceptionstudy. Java: 14) Originating the exception in F () caught in rethrownew, E. printstacktrace () Java. lang. exception: thrown from F () at com. chq. study. throwableexceptionstudy. F (throwableexceptionstudy. java: 40 ) At com. chq. Study. throwableexceptionstudy. rethrownew (throwableexceptionstudy. Java: 63 ) At com. chq. Study. throwableexceptionstudy. Main (throwableexceptionstudy. Java: 15 ) Java. Lang. Exception: From rethrownew at com. chq. Study. throwableexceptionstudy. rethrownew (throwableexceptionstudy. Java: 69 ) At com. chq. Study. throwableexceptionstudy. Main (throwableexceptionstudy. Java: 15) Java. Lang. runtimeexception: from F1 () at com. chq. Study. throwableexceptionstudy. F1 (throwableexceptionstudy. Java: 80 ) At com. chq. Study. throwableexceptionstudy. runtimeexceptiontest (throwableexceptionstudy. Java: 88 ) At com. chq. Study. throwableexceptionstudy. Main (throwableexceptionstudy. Java: 16 ) Java. Lang. Error: custome error, not exception. At com. chq. Study. throwableexceptionstudy. Error (throwableexceptionstudy. Java: 98 ) At com. chq. Study. throwableexceptionstudy. errortest (throwableexceptionstudy. Java: 106 ) At com. chq. Study. throwableexceptionstudy. Main (throwableexceptionstudy. Java: 17)

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.