When I was first learning Java exception handling, I remember a few sentences about the handling of parent-son exceptions: "A child class method can only throw exceptions or child exceptions thrown by the parent class method, and the child class constructor must throw exceptions or parent exceptions of the parent class constructor." At that time, I didn’t know why the subclass method had to throw exceptions like this. Later, by learning "Thinking in Java", I understood the truth. Now I will review it.
1. Subclass methods can only throw exceptions of parent class methods or their child exceptions.
For this limitation, it is mainly because subclasses cannot correctly catch exceptions when they are undergoing 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{ //Subclass methods can throw exceptions of parent class method @Override public void walk() throws BaseException{} //But exceptions that are not in the parent class cannot be thrown, otherwise there will be an error in compilation //public void walk() throws Exception{} //Subclass methods can throw child exceptions of parent class method @Override public void sing() throws SubException1{} //When the implemented interface and methods in the parent class have exceptions, you can choose not to throw exceptions public void task(){}}Take this example as an example. If there is this method in the subclass public void walk() throws CupException{}, an exception that the parent class method does not have is thrown. We use the reference of the parent class to point to the child class.
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 a CupException, so the f() method does not know how to catch the exception. Therefore, during the compilation period, subclass methods should be prevented from throwing exceptions randomly. }From the above example, we can also see that the subclass method can not throw exceptions
2. The subclass constructor must throw an exception or its parent exception.
This is because the subclass constructor is added by default to the parent class constructor.
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 constructor of the parent class is added by default in the subclass constructor, so an exception of the parent class or its parent exception needs to be thrown//super(); }}3. Abnormal loss
1. Throw an exception in finally, the exception thrown may be lost.
package thinkinginjava;public class FinallyException { public static void main(String[] args){ try{ try{ throw new RedException(); } finally{ //Cover 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, no exception is thrown
package thinkinginjava;public class ReturnException { public static void main(String[] args){ try{ throw new Exception(); } finally{ return; } }}We see that it throws an exception, but there will be no output when running
The above article briefly discusses Java exception handling (father-son exception handling) is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.