Exception Handling in java

The Multiply class, which we have seen in Chapter 1, multiplies two integers supplied as arguments and outputs the result.

Multiply
public class Multiply
{

public static void main(String[] args)
{
String resultString;
int arg1;
int arg2;
int result;
arg1 = Integer.ParseInt (args[0]);
arg2 = Integer.ParseInt (args[1]);
result = arg1 * arg2;
resultString = Integer.toString(result);
System.out.println(''The product of " + args[0] + " and " + args[1] + " is " + resultString);
}
}

If, instead of integers, we supply a real number as an argument, Java will raise the following runtime NumberFormatException:

> java Multiply 7.3 8
java.lang.NumberFormatException: 7.3
at java.lang.Integer.parseInt(Integer.java: 344)
at java.lang.Integer.parseInt(Integer.java:382)
at Multiply.main(Multiply.java:9)

Furthermore, if we were to run the program without supplying any arguments, we would get the following ArrayIndexOutofBoundsException:

>javaMultiply
java.lang.ArrayIndexOutofBoundsException: 0
at Multiply.main(Multiply.java:9)


In both cases, the runtime error is caused when executing the statement in line 9:

arg1 = Integer.parseInt(args[0]);

The NumberFormatException is caused by attempting to convert a string containing a real number to an integer. The ArrayIndexOutofBoundsException is caused by the size of the args array being zero; consequently, the element args[0] is outside the bounds of the args array.

The try and catch statements allow exceptions to be handled by the program. The try statement contains all the code that may throw an exception. Each exception is handled by a catch statement. When an exception is thrown at runtime, the try block execution is terminated and control is passed to the appropriate catch statement. The form of try and catch statements is


try
{
one or more statements that may throw an exception
}
catch (Exception e)
{
one or more statements to be executed if this exception is thrown
}

The second version of Multiply has added exception-handling statements.
Multiply—second version

public class Multiply {
public static void main(String[] args) {
String resultString;
int arg1;
int arg2;
int result;
arg1 = Integer.ParseInt (args[0]);
arg2 = Integer.ParseInt (args[1]);
result = arg1 * arg2;
resultString = Integer.toString(result);
System.out.println(''The product of " + args[0] + " and " + args[1] + " is " + resultString);
}
}


The finally statement defines a block of code that is guaranteed to execute after leaving the
try block regardless of how we leave it. Consider the following code fragment:

try
{
one or more statements that may throw an exception
}
catch (Exception1 e)
{
code to execute if Exception1 is thrown, statement a
}
catch (Exception2 e)
{
code to execute if Exception2 is thrown, statement b
}
finally
{
code guaranteed to execute, statement c
}
next statement, statement d
}


If no exception is thrown, then after executing the try block, statements c then d are executed. If either Exception1 or Exception2 are thrown, then either statement a or b will be executed. Control is then passed to statement c, then d. At this point, the

finally statement may seem redundant; after all, statement c could be added to the same block as, and immediately prior to, statement d. However, there is a possibility of another runtime exception, Exception3, being thrown. Although we should try to anticipate likely runtime exceptions in our code, it may not be practical to do so. In this case, the program will abort with an error message at some point in the try block before reaching statement d. However, if we have the finally clause in our code, statement c will execute even if Exception3 is thrown before the program aborts. Typically, statement c would contain some sort of tidying-up code. For example, a file may be opened by one of the statements in the try block. The file may still be open when one of the exceptions is thrown. The statement(s) in the finally block would close the file if it were still open.

No comments:

Post a Comment