When I was learning Java exception handling, I remember a few words about the handling of a parent-child exception. A subclass method can only throw an exception or its child exception that the parent class method throws, and the child class constructor must throw an exception or its parent exception to the parent class constructor. At that time still do not know why the subclass method to throw an exception, and later by learning "thinking in Java", I understand the truth, now I have to review.
A subclass method can only throw the exception of the parent class method or its child exception
For this limitation, the main reason is that the subclass does not catch the exception correctly when doing an upward transition
Package Thinkinginjava;
Public abstract class InningDemo1 {public
void Walk () throws baseexception{} public
abstract void Sing () throws B aseexception;
}
Class Baseexception extends exception{}
class SubException1 extends baseexception{}
class Cupexception extends exception{}
Package Thinkinginjava;
Public interface Otherfunction {public
void task () throws runtimeexception;
Package Thinkinginjava;
The public class SubInningDemo1 extends InningDemo1 implements otherfunction{
//Subclass method can throw exceptions to the parent class method
@Override
public void Walk () throws baseexception{}
//But cannot throw exceptions that are not in the parent class, otherwise the compilation will error
//public void Walk () throws exception{}
A subclass method can throw a child exception of a parent class method
@Override public
void Sing () throws subexception1{}
//When the implemented interface and the methods in the parent class have exceptions, you can choose not to throw an exception Public
Void Task () {}
}
Take this example, if the subclass has this method public void walk () throws cupexception{}, throws the exception that the parent class method does not have, we use the reference of the parent class to point to the subclass
public void F () {
InningDemo1 inn = new SubInningDemo1 ();
Inn.walk ();//The parent class calls the walk () method without knowing that it throws a cupexception, thus the F () method does not know how to catch the exception. Therefore, it is necessary to prevent the subclass method from throwing the exception at compile time.
}
From the example above, we can also see that the subclass method can not throw an exception
Second, the subclass constructor must throw the exception of the parent class constructor or its parent exception
This is because the constructor of the parent class is added by default in the subclass 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 you need to throw the exception of the parent class or its parent exception
//super ();
}
Third, abnormal loss
1, throw an exception in finally, may be thrown before the exception is lost
Package Thinkinginjava;
public class Finallyexception {public
static void Main (string[] args) {
try{
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{}
Run Result: Thinkinginjava. Blueexception
2. Use return in Finally, do not throw exception
Package Thinkinginjava;
public class Returnexception {public
static void Main (string[] args) {
try{
throw new Exception ();
} finally{return;}}
The above code we see it throws an exception, but the runtime does not have any output
This article on the Java exception handling (father and son exception processing) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.