Java Exception Handling (parent-child Exception Handling) and java Exception Handling
When I first learned how to handle a java exception, I remember a few words for the parent and child Exception Handling: "The subclass method can only throw the exception thrown by the parent class method or its subexceptions, the subclass constructor must throw an exception or parent exception of the parent class constructor ". At that time, I still don't know why the subclass method throws an exception like this. Later I learned Thinking in Java to understand the truth. Now I will study it again.
1. Subclass methods can only throw exceptions of parent methods or child exceptions
This restriction is mainly because the subclass cannot correctly capture exceptions when performing the upward transformation.
package thinkinginjava;public abstract class InningDemo1 { public void walk() throws BaseException{} public abstract void sing() throws BaseException;}class BaseException extends Exception{}class SubException1 extends BaseException{}class CupException extends Exception{}
package thinkinginjava;public interface OtherFunction { public void task() throws RuntimeException;}
Package thinkinginjava; public class SubInningDemo1 extends InningDemo1 implements OtherFunction {// The subclass method can throw an exception of the parent class method @ Override public void walk () throws BaseException {} // but cannot throw exceptions that are not present in the parent class. Otherwise, compilation will fail. // public void walk () throws Exception {} // sub-class method can throw a sub-Exception of the parent class method @ Override public void sing () throws SubException1 {}// when both the implemented interface and the method in the parent class have an exception, you can choose not to throw an exception public void task (){}}
Take this example as an example. If the subclass has the public void walk () throws CupException {} method, an exception not found in the parent class method is thrown. We use the reference of the parent class to point to the subclass.
Public void f (){
InningDemo1 inn = new SubInningDemo1 ();
Inn. walk (); // when the parent class calls the walk () method, it does not know that it will throw CupException, so the f () method does not know how to capture the exception. Therefore, during the compilation period, the subclass method should be prevented from throwing exceptions.
}
In the preceding example, we can also see that the subclass method does not throw an exception.
2. The subclass constructor must throw an exception or parent exception of the parent class constructor.
This is because the parent class constructor is added to the subclass constructor by default.
package thinkinginjava;public abstract class InningDemo2 { public InningDemo2() throws SubException{ }}class FatherException extends Exception{}class SubException extends FatherException{}class PenException extends Exception{}
Package thinkinginjava; public class SubInningDemo2 extends InningDemo2 {public SubInningDemo2 () throws FatherException {// The parent class constructor is added by default to the subclass constructor, therefore, an exception of the parent class or its parent exception must be thrown. // super ();}}
Iii. Exception loss
1. if an exception is thrown in finally, the exception thrown earlier may be lost.
Package thinkinginjava; public class FinallyException {public static void main (String [] args) {try {throw new RedException ();} finally {// overwrite the previous Exception throw new BlueException () ;}} catch (Exception e) {System. out. println (e) ;}} class RedException extends Exception {} class BlueException extends Exception {}
Running result: thinkinginjava. BlueException
2. Use return in finally without throwing an exception
package thinkinginjava;public class ReturnException { public static void main(String[] args){ try{ throw new Exception(); }finally{ return; } }}
The code above shows that it throws an exception, but there is no output at runtime.