Java knows how much (in) the nesting of Try statements

Source: Internet
Author: User

Try statements can be nested. In other words, a try statement can be inside another try block. Each time you enter a try statement, the exception's front and back relationships are pushed onto the stack. If an internal try statement does not contain a catch handler for a special exception, the stack pops up and the catch handler for the next try statement checks to see if it matches. This process will continue until a catch statement is successfully matched, or until all nested try statements are checked for exhaustion. If there is no catch statement match, the Java Runtime system will handle the exception. Here is an example of using a nested try statement:

1 //An example of nested try statements.2 classNesttry {3      Public Static voidMain (String args[]) {4         Try {5             intA =args.length;6             /*If No command-line args is present,the following statement would generate a Divide-by-zero exception.*/7             intb = 42/A;8System.out.println ("a =" +a);9             Try{//Nested try blockTen                 /*If One command-line arg is used,then a Divide-by-zero exception would be generated by the following code.*/ One                 if(a==1) A = A/(A-A);//Division by Zero A                 /*If The command-line args is Used,then generate an out-of-bounds exception.*/ -                 if(a==2) { -                     intC[] = {1 }; theC[42] = 99;//generate an out-of-bounds exception -                 } -}Catch(arrayindexoutofboundsexception e) { -SYSTEM.OUT.PRINTLN ("Array index out-of-bounds:" +e); +             } -}Catch(ArithmeticException e) { +System.out.println ("Divide by 0:" +e); A         } at     } -}

As you can see, the program nested another try block in a try block. The program works as follows: When you execute the program without command-line arguments, the outside try block produces an exception that is removed by 0. The program executes under a command-line parameter condition, resulting in an error that is removed by 0 by a nested try block. Because the inner block does not match this exception, it passes the exception to the external try block, where the exception is handled. If you execute the program under conditions that have two command-line arguments, an array boundary exception is generated by an internal try block. The following results illustrate each of these situations:

C:\>0:java.lang.arithmeticexception:/by zeroc:\>= 10: Java.lang.ArithmeticException:/by zeroc:\>= 2Array indexout-of-bounds: Java.lang.ArrayIndexOutOfBoundsException

When there is a method call, the nesting of the try statement can occur very covertly. For example, you can put a call to a method in a try block. Inside the method, there is another try statement. In this case, the try inside the method is still nested within the try block that calls the method externally. The following is a modification of the previous example, and the nested try block is moved to the inside of Method Nesttry ():

1 /*Try statements can implicitly nested via calls to methods.*/2 classMethnesttry {3     Static voidNesttry (inta) {4         Try{//Nested try block5             /*If One command-line arg is used,then a Divide-by-zero exception would be generated by the following code.*/6             if(a==1) A = A/(A-A);//Division by Zero7             /*If The command-line args is Used,then generate an out-of-bounds exception.*/8             if(a==2) {9                 intC[] = {1 };TenC[42] = 99;//generate an out-of-bounds exception One             } A}Catch(arrayindexoutofboundsexception e) { -SYSTEM.OUT.PRINTLN ("Array index out-of-bounds:" +e); -         } the     } -  -      Public Static voidMain (String args[]) { -         Try { +             intA =args.length; -            /*If No command-line args is present,the following statement would generate a Divide-by-zero exception.*/ +            intb = 42/A; ASystem.out.println ("a =" +a); at Nesttry (a); -}Catch(ArithmeticException e) { -System.out.println ("Divide by 0:" +e); -         } -     } -}

The output of the program is the same as the previous example.

Series Articles:

Java know how much (top)

Java know how much (interface) interface

Java knows how much (40) the difference between an interface and an abstract class

Java know how much (41) generic explanation

Java know how much (42) the range of generic wildcard characters and type parameters

Java know how much (43) Exception Handling Basics

Java know how much (44) exception type

Java know how much (45) uncaught exceptions

How much Java knows (the) use of try and catch

Java know how much (47) use of multiple catch statements

Java knows how much (in) the nesting of Try statements

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.