Propagation of Exceptions in java

MultiplyClass now consists of a main method, which accepts two integer arguments and invokes the multiplyHandler method. multiplyHandler then invokes the multiply method, which as we have seen in the previous section may throw an ArgumentTooBig exception.
In this section, we examine how the exception thrown in the multiply method is propagated upwards through the multiplyHandler and main methods.

MultiplyClass

public class MultiplyClass
{
Public static void main(String[] args)
throws ArgumentTooBigException)
{
int i, resultString;
int arg1;
int arg2;
int result;
arg1=Iteger.parseInt(args[0]);
arg2=Integer.parseint(args[1]);
result = multiplyHandler (arg1, arg2);
resultString = Integer.toString(result);
System.out.println(''The product of " + args[0]
+ " and " + args[1] + " is " + resultString);
}

public static int multiply(int arg1, int arg2)
throws ArgumentTooBigException
{
if (arg1 > 99 | arg2 > 99)
{

throw new ArgumentTooBigException();
}
return arg1 * arg2;
}

public static int multiplyHandler(int arg1, int arg2)

throws ArgumentTooBigException {
return multiply(arg1, arg2);
}
}


Since the invoked multiply method specifies the ArgumentTooBigException in its throws clause, this exception must be either caught or specified in the throws clause of
the multiplyHandler method. Since multiplyHandler does not catch the exception, we must specify it in the throws clause of the method (line 27).

Now consider the code for the main method. Since the invoked multiplyHandler method specifies ArgumentTooBigException in its throws clause, this exception must be either caught or specified in the throws clause of the main method. Since the main method does not catch the exception, we must specify it in its throws clause (line 4).

Although we have thrown an ArgumentTooBig exception in the multiply method, we have made no attempt to catch the exception using the try catch construct. What happens if the exception is thrown? Java will work through the method call stack, through the multiply, multiplyHandler, and main methods in turn, searching for an exception handler. Since no exception handler is found, the runtime system, and so the Java program, terminates. The following output shows the ArgumentTooBig exception being thrown:

> java MultiplyClass 100 98
ArgumentTooBigException
at MultiplyClass.multiply
at MultiplyClass.multiplyHandler
at MultiplyClass.main


It is not good practice to throw an exception from the main method and have the runtime system terminate the program. The application should catch the exception and terminate the program in a controlled manner. The second version of MultiplyClass has the main method modified to use the try catch construct.

MultiplyClass—second version

public class MultiplyClass
{

public static void main(String[] args)
{
String resultString;


int arg1;
int arg2;
int result;

try
{

arg1=Iteger.parseInt(args[0]);
arg2=Integer.parseint(args[1]);
result = multiplyHandler (arg1, arg2);
resultString = Integer.toString(result);
System.out.println(''The product of " + args[0]
+ " and " + args[1] + " is " + resultString);
}
catch (ArgumentTooBigException e)
{
System.out.println("arguments must be < 100");
System.out.println(e.toString() );
}
}

public static int multiply(int arg1, int arg2)
throws ArgumentTooBigException
{
if (arg1 > 99 | arg2 > 99)
{
throw new ArgumentTooBigException();
}
return arg1 * arg2;
}

public static int multiplyHandler(int arg1, int arg2)
throws ArgumentTooBigException
{
return multiply(arg1, arg2);
}
}


Recall that the ArgumentTooBig exception must be either caught or specified in the throws clause of the main method. Since the exception is caught, we do not need to specify it in the throws clause.

If we now run MultiplyClass, we get the following result:

> java MultiplyClass 100 98
arguments must be < 100
ArgumentTooBigException


Rather than printing the trace of the method call stack, only the code within the catch group of statements is executed.

No comments:

Post a Comment