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
5. Abnormal loss