Java multi-layered exception capture

Source: Internet
Author: User

Example program One:

 Public classcatchwho { Public Static voidMain (string[] args) {Try {             Try {                 Throw Newarrayindexoutofboundsexception (); }             Catch(arrayindexoutofboundsexception e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/Inner layer Try-catch"); }             Throw NewArithmeticException (); }         Catch(ArithmeticException e) {System.out.println ("Happened ArithmeticException"); }         Catch(arrayindexoutofboundsexception e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/outer try-catch"); }     } }

Results:

Cause analysis: Because there are two throws in the program that throw two different types of errors, the catch that follows immediately catches the corresponding type of error and performs the appropriate processing.

Because

Example Program 2:

 Public classCatchWho2 { Public Static voidMain (string[] args) {Try {            Try {                 Throw Newarrayindexoutofboundsexception (); }             Catch(ArithmeticException e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/Inner layer Try-catch"); }            Throw NewArithmeticException (); }         Catch(ArithmeticException e) {System.out.println ("Happened ArithmeticException"); }         Catch(arrayindexoutofboundsexception e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/outer try-catch"); }     } }

Results:

Conclusion:

There can be more than one catch statement block, and each block of code captures an exception. It is a syntax error to have two different catch blocks after a try block to catch two of the same type of exception.

With a catch statement, you can only capture objects of the exception class and its subclasses. Therefore, a catch statement block that captures the exception object captures all the "catch" exceptions.

placing catch (Exception e) in front of other catch blocks causes these catch blocks to not execute, so Java does not compile the program.

Java multi-layered exception capture

Related Article

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.