The throw and throws Keywords


The throw and throws Keywords

One of the nice features about Java exception handling is that you don't have to handle an exception where it occurs. You can pass the exception to another method and let the second method take care of it. Being able to pass the exception handling to another piece of code is useful if you don't want to hard-code exception handling into a given method.

An exception is explicitly thrown using the throw statement followed by an instance of the exception class to be thrown. This is the mechanism used by all exception-throwing methods in the Java API. A thrown exception is either handled in a catch clause or sent to the calling method of the method that threw the exception. The method that receives a thrown exception can either process the exception or re-throw it to another method.

Most of the time, the system will generate and throw the necessary exceptions. If you like, you can manually throw an exception using the throw statement followed by the object to be thrown. The general syntax to manually throw an exception is

 throw throwableObject; 

The object thrown must be a subclass of Throwable . This syntax can also be used to rethrow a caught exception.

The throws clause is used to declare the exception types that a method can throw. Java requires that methods either catch or declare all checked exceptions that can be thrown within the scope of the method. If appropriate handlers are not provided inside the method, the throws keyword followed by the types of checked exceptions that can be thrown must be included in the method declaration.

 [modifiers] return_type method_name (argument_list)            throws exception_type1, exception_type2, ... 

This declaration requirement only applies to checked exceptions. You do not have to declare the unchecked exceptions a method can throw.

Example: Passing Exceptions to Another Method

It is usually undesirable to hard-code exception handling into a method that is meant to be used by many different applications. A better solution is to leave the details of the exception handling up to the individual application by throwing any exceptions back to the calling method.

The ArrayFunctions class defines the static method scaleArray() that divides the elements of a one-dimensional array by a specified value. A floating point divided by zero will not cause the system to throw an exception, but will instead return a garbage value such as infinity or NaN . To avoid this possibility, we will have the method throw an ArithmeticException if the method argument d has the value 0.0 . Because an ArithmethicException is a RuntimeException , we do not have to declare it using the throws clause in the method declaration.

 public class ArrayFunctions {   public static void scaleArray(                 double[] array, double d)   {     if ( d == 0.0 ) throw new ArithmeticException();     for(int i=0; i<array.length; ++i) {       array[i] /= d;     }   } } 

The ThrowDemo class creates a 1-D area of double values and calls the scaleArray() method. Because the scaleArray() method can throw an ArithmeticException , the method call is placed inside a block of code after a try statement. Since we are passing the scaleArray() method the value 0.0 , an exception will be thrown and caught by the catch clause. Try running this code as it is listed and also try passing the scaleArray() method a non-zero input value.

 public class ThrowDemo {   public static void main(String args[]) {     double data[] = { 1.2, 2.3, 3.4 };     try {       ArrayFunctions.scaleArray(data, 0.0);       printArray(data);     } catch(ArithmeticException ae) {         System.out.println("oops, divide by zero");     }   }   public static void printArray(double array[]) {     for(int i=0; i<array.length; ++i) {       System.out.println("array[" + i +                 "] = " + array[i]);     }     System.out.println();   } } 

Output ”

 oops, divide by zero 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net