Java not caught exception
Before you learn to handle exceptions in a program, it's good to look at what happens if you don't deal with them. The following applet includes an expression that intentionally causes a 0 error to be removed.
Class Exc0 {public
static void Main (String args[]) {
int d = 0;
int a = 42/d;
}
}
When the Java runtime system checks for a 0 addition, it constructs a new exception object and then throws the exception. This causes the execution of the Exc0 to stop because once an exception is thrown, it must be caught by an exception handler and processed immediately. In this example, we did not provide any of our own exception handlers, so the exception was caught by the default handler for the Java Runtime system. Any exception that is not caught by your program will eventually be handled by the default handler. The default handler displays a string that describes the exception, prints the stack trajectory at which the exception occurred, and terminates the program.
The following is the output produced by the standard JAVAJDK runtime interpreter executing the program:
Java.lang.ArithmeticException:/by zero at
Exc0.main (exc0.java:4)
Note that class name Exc0, method name main, filename Exc0.java and row number 4 are included in a simple stack usage trajectory. Also, note that the exception type thrown is a subclass of exception called ArithmeticException, which describes the type of error method more specifically. As discussed later in this chapter, Java provides several built-in exception types that match the different kinds of run-time errors that may occur.
The stack trajectory will show the sequence of method calls that caused the error to occur. For example, the following is another version of the previous program that describes the same error, but the error is generated in another method other than the main () method:
Class Exc1 {
static void subroutine () {
int d = 0;
int a = 10/d;
}
public static void Main (String args[]) {
exc1.subroutine ();
}
}
The stack trajectory result for the default exception handler shows how the entire call stack is displayed:
Java.lang.ArithmeticException:/by zero in
exc1.subroutine (exc1.java:4) at
Exc1.main (exc1.java:7)
As you can see, the bottom of the stack is line 7th of Main, which calls the subroutine () method. The method causes an exception on line 4th. The call stack is important for debugging because it identifies the exact steps that led to the error.
Nesting of Java try Statements
The try statement can be nested. In other words, a try statement can be inside another try block. Each time you enter a try statement, the relationship of the exception is 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 continues until a catch statement is matched successfully, or until all the nested try statements are exhausted. If no catch statement matches, the Java runtime system will handle this exception. Here is an example of using a nested try statement:
/An example of nested try statements. class Nesttry {public static void main (String
Args[]) {try {int a = Args.length; /* If No command-line args are present,the following statement'll generate a Divide-by-zero exception.
*/int b = 42/a;
System.out.println ("a =" + a); try {//nested try block/* If One command-line arg is used,then a Divide-by-zero exception'll be generated by The following code. */if (a==1) A = A/A (A-A); 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.prin
TLN ("Array index out-of-bounds:" + e);
The catch (ArithmeticException e) {System.out.println ("Divide by 0:" + E); }
}
}
As you can see, the program has 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 0 apart. The program executes with a command-line parameter, and a nested try block produces an error that is 0 removed. Because the inner block does not match this exception, it passes the exception to the outer try block, where the exception is processed. If you execute the program under conditions with two command-line arguments, an array boundary exception is generated from the inner try block. The following results illustrate each of these situations:
C:\>java nesttry
Divide by 0:java.lang.arithmeticexception:/by zero
C:\>java nesttry one
a = 1
D Ivide by 0:java.lang.arithmeticexception:/by zero
C:\>java Nesttry One Two
a = 2
Array index Out-of-boun Ds: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 in the method is still nested inside a try block that calls the method externally. The following is a modification of the previous example, where the nested try block is moved inside the method Nesttry ():
/* Try statements can is implicitly nested via calls to methods. */class Methnesttry {static void nesttry (int a) {try {//nested try block/* If One command-line arg is US Ed,then A Divide-by-zero exception is generated by the following code. */if (a==1) A = A/A (A-A); 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 ("A
Rray 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'll 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.