Java: Exceptions – Throwing

Posted February 15, 2006 by William_Wilson in Java programming

The following tutorial explains the throwing of exceptions to be caught elsewhere.


It should be noted that all exceptions that can be generated are subclasses of the class java.lang.throwable.Exception. With this in mind and the idea of a hierarchy of errors, it is possible to write effective and working throwable exceptions, combined with a knowledge of try and catch blocks.

When dealing with throwing exceptions, the somewhere in the trace of method calls, the error must be caught. Although before try and catch blocks are needed, they are not needed in every method.

CODE example:
public class ExceptionTester5 {
private static int quotient(int numerator, int denominator) throws ArithmeticException {
if (denominator == 0)
throw new ArithmeticException();
return(numerator / denominator);
}

public static void main(String args[]) {
int number1=0, number2=0, result=0;
try {
number1 = 1;
number2 = 0;
result = quotient(number1,number2);
System.out.print(number2 + ” goes into ” + number1);
System.out.println(” this many times: ” + result);
}
catch (Exception e) {
System.out.println(e.toString());
System.out.println(“An Exception occured”);
System.exit(-1);
}
}
}

In this example, the exception occurs in the method quotient, but since this method declared in the first line that it throws ArithmeticException, it is able to do so within the body of its code and then the case where the denominator is 0 handled and if so, throw new ArithmeticException();. By doing this, it creates a new Exception Object. Since there is no catch block within this method, the exception is truly thrown and is then caught by the method which called it – main.

It is still a requirement to include the try/catch blocks, but they may be within a single method. All methods called from the try/catch block will be thrown back to this method when exceptions are thrown.

NOTE: It is important to remember that an error can throw itself. The case of finding the 0 denominator is not necessary:

CODE re-written:
public class ExceptionTester5 {
private static int quotient(int numerator, int denominator) throws ArithmeticException {
return(numerator / denominator);
}

public static void main(String args[]) {
int number1=0, number2=0, result=0;
try {
number1 = 1;
number2 = 0;
result = quotient(number1,number2);
System.out.print(number2 + ” goes into ” + number1);
System.out.println(” this many times: ” + result);
}
catch (Exception e) {
System.out.println(e.toString());
System.out.println(“An Exception occured”);
System.exit(-1);
}
}
}

*Here, we reach the operation of dividing by 0, and the exception is created and thrown back to the main method. The output is exactly the same as the previous.
Since we informed Java that this method can throw an ArithmeticException, it does so when it reaches such a case.
In the previous example when WE handled the exception, we could have chosen nearly any exception to throw, as we declared what we wanted the error to be. This opens the door for writing our own exceptions since we can declare the name of the object.

Questions/Comments: [email protected]
-William. ยง (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.