5. Abnormal loss

Source: Internet
Author: User

5. Exception loss Scenario One: Catch exception, throw new exception

This situation is described in the previous article, the following code in the F () function throws an exception will be lost, you can use Exception ex=new Exception (); Ex.initcause (e); reserved exceptions

Try {     f ();  } Catch (Exception e) {     thrownew  Exception ();    }
Case two: There is a return statement in finally

The exception thrown in F () is not caught

Try {    f ();} finally {//try not to catch exceptions    using catch words behind return                                                                                      ;} 
Case two: The exception thrown in finally will lose the previous exception
Try {      try{            f ();      } finally {            g ();      }} Catch (Exception e) {      e.printstacktrace ();}   

Summarize:

1. Only exceptions in G () can be captured and exceptions in F () are not captured

Best not to throw exceptions in 2.finally

Exception Limits

1. The parent Class A has method F ()-throws an exception E1 subclass B has method F ()--a. Can throw an exception E1 B. can throw an exception E2 (E1 subclass) c. Do not throw an exception

2. Throwing a subclass exception can catch the exception of the parent class, and vice versa.

Example:

classBaseexceptionextendsexception{}classChildexceptionextendsbaseexception{}Abstract classinning{ Public Abstract voidAtBat ()throwsbaseexception;}classStorminningextendsinning{ Public voidAtBat ()throwschildexception{Throw Newchildexception ();}}
inheritance implements exception handling of interfaces---important knowledge points
Interface type1{    void F ()throws  clonenotsupportedexception;} Interface type2{    void F ()throws  interruptedioexception;} class Implements type1,type2{----If you implement only one interface, you can not throw an exception, or throw an exception thrown in the    interface and its subtype public void  f () {} The f () here must throw the intersection of the exceptions in the Type1,type2 (or you can not throw exceptions)

fine-grained handling of anomalies

  • Nesting with try blocks, graded catch exceptions
  • classreadfirstline{ PublicReadfirstline () {Try{BufferedReader in=NULL; Try{ in=NewBufferedReader (NewFileReader (NewFile ("Test.iml"))); } Catch(FileNotFoundException e) {e.printstacktrace ();//do not need to close file} String firstline =In.readline ();        System.out.println (Firstline); } Catch(Exception e) {
    In.close ();//Close output stream e.printstacktrace (); } finally { } }}

5. Abnormal loss

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.