Java exception handling (handling of parent-child exceptions)

Source: Internet
Author: User

When I learned about Java exception handling, I remember a few words about the handling of parent-child exceptions: The subclass method can only throw exceptions that are thrown by the parent class method, or its child exceptions, and the subclass constructor must throw the parent class constructor exception or its parent exception. At that time do not know why the sub-class method to throw an exception, and later through the study of "Thinking in Java", I understand the truth, and now I will review.

A subclass method can only throw an exception of a parent class method or its child exception

  This limitation is mainly due to the fact that the subclass does not catch the exception correctly when it is doing an upward transformation

 PackageThinkinginjava; Public Abstract classInningDemo1 { Public voidWalk ()throwsbaseexception{} Public Abstract voidSing ()throwsbaseexception;}classBaseexceptionextendsexception{}classSubException1extendsbaseexception{}classCupexceptionextendsexception{}
 Package Thinkinginjava;  Public Interface otherfunction {    publicvoidthrows  runtimeexception;}  

 PackageThinkinginjava; Public classSubInningDemo1extendsInningDemo1Implementsotherfunction{//subclass methods can throw exceptions to the parent class method@Override Public voidWalk ()throwsbaseexception{}//But you cannot throw exceptions that are not in the parent class, or the compilation will go wrong//Public Void Walk () throws exception{}//a subclass method can throw a child exception of a parent class method@Override Public voidSing ()throwssubexception1{}//you can choose not to throw exceptions when both the implemented interface and the methods in the parent class have exceptions     Public voidtask () {}}

  Take this example, if the subclass has this method public void walk () throws cupexception{}, throws the exception that the parent class method does not, we use the reference of the parent class to point to the child class

public void F () {

InningDemo1 inn = new SubInningDemo1 ();

Inn.walk ();//The parent class calls the walk () method without knowing that it throws cupexception, so the F () method does not know how to catch the exception. Therefore, it is necessary to prevent the subclass method from throwing out the exception at compile time.

}

 From the above example, we can also see that the subclass method can not throw an exception

Second, the subclass constructor must throw the parent class constructor exception or its parent exception

  This is because the constructor for the parent class is added by default in the Child class constructor

 package   Thinkinginjava;  public  abstract  class   InningDemo2 { public  InningDemo2 () throws   subexception{}}  class  Fatherexception extends   exception{}  Span style= "color: #0000ff;" >class  subexception  fatherexception{}  class  penexception  exception{} 
 Package Thinkinginjava;  Public class extends inningdemo2{    publicthrows  fatherexception {        //  The constructor for the parent class is added by default in the subclass constructor, so you need to throw the parent exception or its        parent exception //super ();     }}

Third, abnormal loss

1, throws an exception in finally, may be thrown before the exception is lost

 PackageThinkinginjava; Public classfinallyexception { Public Static voidMain (string[] args) {Try{            Try{                Throw Newredexception (); }finally{                //overwrite the last exception                Throw Newblueexception (); }        }Catch(Exception e) {System.out.println (e); }    }}classRedexceptionextendsexception{}classBlueexceptionextendsexception{}

Running result: Thinkinginjava. Blueexception

2. Use return in Finally, no exception is thrown

 Package Thinkinginjava;  Public class returnexception {    publicstaticvoid  main (string[] args) {         Try {            thrownew  Exception ();        } finally {            return;}}    }

The above code we see it throws an exception, but the runtime does not have any output

Java exception handling (handling of parent-child exceptions)

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.