Nested Java try statement

Try statement can be nested. That is, one may try statement inside another try block. Each entered try statement, unusual context will be pushed onto the stack. If a try statement inside the catch handler without special exception, will pop the stack, the next catch handler checks to see if the try statement match. This process will continue until a catch clause matches, or until all the nested try statements are exhausted checked. If there is no catch statement matches, Java run-time system will handle the exception. The following is an example of the use of nested try statement:
. An Example of // try statements nested
class NestTry {
public static void main (String args []) {
try {
int args.length = A;
/ * the If NO Command-Line Present are args, Will Generate The following Statement A Divide-by-ZERO Exception * /.
int = 42 is B / A;
System.out.println ( "A =" + A);
the try {// the try nested Block
/ * the If IS-Arg Command Line One Used, the then A Divide-by-ZERO Exception Will BE Generated by The following code * /.
IF (A ==. 1) A = A / (AA); // Division by ZERO
/* If two command-line args are used,then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[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);
}
}
}

As you can see, the program further nested try block in a try block. Program works as follows: When you execute the program in the case where no command line parameters, outside a try block generated by zero exception. In the case of a program command line parameters conditional execution, an error is zero by the nested try block. Because the interior of the block does not match this exception, the exception pass it outside the try block, an exception is processed there. If you have two command line parameters under the conditions of executing the program, a try block generated by the internal array bounds exception. The results are set forth below in each case:
C:> Java NestTry
Divide by 0: java.lang.ArithmeticException: / ZERO by
C:> One Java NestTry
A. 1 =
Divide by 0: java.lang.ArithmeticException: / ZERO by
C:> Java NestTry One Two
A 2 =
the Array index-of-bounds OUT: java.lang.ArrayIndexOutOfBoundsException

When a method is called, nested try statements can be very subtle occur. For example, you can put in a call to the method try block. In the interior of the method, another try statement. In this case, the inside of the method is still try nested try block external calls in the process. The following is a modification of the previous example, the nested try block moved method nesttry () internal:
. / * Implicitly the Try statements nested CAN BE Via Calls to Methods * /
class MethNestTry {
static void nesttry (int A) {
try {/ / nested Block the try
/ * the If Command-Line One IS Used Arg, by the then-A-ZERO Divide Exception Will BE Generated by The following code * /.
IF (A ==. 1) A = A / (AA); // ZERO by Division
/ * args the If TWO Command-Line Used are, the then Generate AN OUT-of-bounds Exception * /.
IF (A == 2) {
int C [] = {}. 1;
C [42 is] = 99; Generate of AN OUT-//-bounds Exception
}
} the catch (An ArrayIndexOutOfBoundsException E) {
System.out.println(“Array index out-of-bounds: ” + e);
}
}

public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,the following statement will generate a divide-by-zero exception. */
int b = 42 / a;
System.out.println(“a = ” + a);
nesttry(a);
} catch(ArithmeticException e) {
System.out.println(“Divide by 0: ” + e);
}
}
}

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

VII. Multithreaded programming
1. The concept of threads
2. the Java threading model
3. The main thread
4. Create a thread
5. Create a multithreaded
6. Use isAlive () and join () of
7. thread priority
8. thread synchronization
9. communication between the threads
10. a thread deadlock
11. the thread suspend, resume and terminate

Guess you like

Origin blog.csdn.net/Javaxuxuexi/article/details/92428298