Throwing Exceptions

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 9.  Exception Handling


These three steps are involved in throwing exceptions:

  1. Publish a list of exceptions that your method can throw

  2. Create an instance of the exception class that describes your error

  3. Throw that exception

Publishing an Exception List

If a method can potentially throw an exception, it must publish it to all parties that call it. This is accomplished by appending the throws clause to the signature of the method. For example:

 public void myMethod() throws MyException 

This statement says that myMethod() can throw the exception MyException, so anyone that calls myMethod must explicitly handle MyException. In other words, if you call myMethod(), you must call it within a try block and provide a catch block that catches MyException or one of its superclasses. If you don't, the compiler will generate an error.

Your method can throw more than one exception, but it must then list all the possible exceptions it can throw in a comma-separated list. For example, if your method can throw MyException1, MyException2, and MyException3, its signature would look as follows:

 public void myMethod throws MyException1, MyException2, MyException3 

Creating an Instance of an Exception

You must define a new class, such as MyException1, for your exception and then create an instance of it when appropriate. Thus, if your method detects an error condition that is represented by MyException1, you create an instance of it in your code:

 ...  if( badCondition ) {     // Create and initialize MyException1     MyException1 e =       new MyException1( "You did not enter the correct value" );     e.setValueEntered( value );     e.setRangeExpected( 1, 10 );  } 

Because exceptions are normal classes that extend Exception, you can add properties and methods freely. In this example, the MyException1 class has a constructor that accepts a String describing the exception, two methods for setting the value that the user entered, and a range of valid values. I will show you a concrete example of creating a custom exception in the "Creating a Custom Exception," section later in the chapter.

Throwing the Exception

When you have an instance of an Exception created, you can throw it using the throw keyword. The throw keyword stops the execution of the method at the point that the throw is called and throws the exception to the caller. For example:

 public void myMethod() throws MyException1  ...  if( badCondition ) {     // Create and initialize MyException1     MyException1 e = new MyException1(        "You did not enter the correct value" );     e.setValueEntered( value );     e.setRangeExpected( 1, 10 );     throw e;  }  // Do something  int i=10;  ...  } 

The exception e is thrown using the throw keyword, and then execution of the method stops; all the statements after the throw statement are not executed. In this case the creation of the integer i is never executed if badCondition evaluates to true.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

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