J.E.D.I.
2.2.3  An Example
Consider the following program: class DivByZero {
public static void mainString args[] { System.out.println30;
System.out.println“Pls. print me.”; }
}
Running the code would display the following error message. Exception in thread main java.lang.ArithmeticException:  by zero at
DivByZero.mainDivByZero.java:3 The message provides the information on the type of exception that has occurred and
the line of code where the exception had originated from. This is how the default handler deals with uncaught exceptions. When no user code handles the occurring excpetion, the
default   handler   goes  to   work.  It   first   prints   out   the   description   of   the   exception   that occured. Moreover, it also prints the stack trace that indicates the hierarchy of methods
where   the   exception   happened.   Lastly,   the   default   handler   causes   the   program   to terminate.
Now, what if you want to handle the exception in a different manner? Fortunately, the Java   programming   language   is   incorporated   with   these   three   important   keywords   for
exception handling, the try, catch and finally keywords.
2.3  Catching Exceptions
2.3.1  The try-catch Statements
As mentioned in the preceding section, the keywords  try,  catch  and  finally  are used to handle  different types of exceptions. These three keywords are used together but the
finally  block is optional. It would be good to first focus on the first two reserved words and just go back to the finally later on.
Given below is the general syntax for writing a try-catch statement. try {
code to be monitored for exceptions } catch ExceptionType1 ObjName {
handler if ExceptionType1 occurs }
... } catch ExceptionTypeN ObjName {
handler if ExceptionTypeN occurs }
Coding Guidelines: The catch block starts after the close curly brace of the preceding try or catch block.
Statements within the block are indented.
Applying this to DivByZero program you’ve studies earlier, class DivByZero {
Introduction to Programming II Page 25
J.E.D.I.
public static void mainString args[] { try {
System.out.println30; System.out.println“Please print me.”;
} catch ArithmeticException exc { reaction to the event
System.out.printlnexc; }
System.out.println“After exception.”; }
}
The divide by zero error is an example of an  ArithmeticException. Thus, the exception type indicated in the catch clause is this class. The program handles the error by simply
printing out the description of the problem.
The output of the program this time would be as follows: java.lang.ArithmeticException:  by zero
After exception. A particular code monitored in the try block may cause more than one type of exception
to occur. In this case, the different types of errors can be handled using several  catch blocks. Note that the code in the try block may only throw one exception at a time but
may cause different types of exceptions to occur at different times.
Here is an example of a code that handles more than one type of exception. class MultipleCatch {
public static void mainString args[] { try {
int den = Integer.parseIntargs[0]; line 4
System.out.println3den; line 5
} catch ArithmeticException exc { System.out.println“Divisor was 0.”;
} catch ArrayIndexOutOfBoundsException exc2 { System.out.println“Missing argument.”;
} System.out.println“After exception.”;
} }
In   this   example,   line   4   may   throw  ArrayIndexOutOfBoundsException  when   the   user forgets   to   input   an   argument   while   line   5   throws   an  ArithmeticException  if   the   user
enters 0 as an argument.
Study what happens to the program when the following arguments are entered by the user:
a No argument b 1
c 0
If no argument was passed, the following output will be displayed: Missing argument.
After exception.
Passing 1 as argument gives the following output: 3
Introduction to Programming II Page 26
J.E.D.I.
After exception. Meanwhile, passing the argument 0 will give this output:
Divisor was 0. After exception.
Nested trys are also allowed in Java. class NestedTryDemo {
public static void mainString args[]{ try {
int a = Integer.parseIntargs[0]; try {
int b = Integer.parseIntargs[1]; System.out.printlnab;
} catch ArithmeticException e { System.out.println“Divide by zero error;
} } catch ArrayIndexOutOfBoundsException exc {
System.out.println“2 parameters are required; }
} }
Analyze what happens to the code when these arguments are passed to the program: a No argument
b 15 c 15  3
d 15  0
Here are the expected output for each sample argument set: a No argument
2 parameters are required b 15
2 parameters are required c 15 3
5 d 15 0
Divide by zero error The code below has a nested try in disguise with the use of methods.
class NestedTryDemo2 { static void nestedTryString args[] {
try { int a = Integer.parseIntargs[0];
int b = Integer.parseIntargs[1]; System.out.printlnab;
} catch ArithmeticException e { System.out.printlnDivide by zero error;
} }
public static void mainString args[]{ try {
nestedTryargs; } catch ArrayIndexOutOfBoundsException e {
System.out.println2 parameters are required; }
}
Introduction to Programming II Page 27
J.E.D.I.
} What is the output of this program when tested on the following arguments?
a No argument b 15
c 15  3 d 15  0
The expected output for NestedTryDemo2 is similar to that of NestedTryDemo.
2.3.2  The finally Keyword