19.9. Key Terms

 
[Page 592 ( continued )]

17.9. (Optional) Creating Custom Exception Classes

Java provides quite a few exception classes. Use them whenever possible instead of creating your own exception classes. However, if you run into a problem that cannot be adequately described by the predefined exception classes, you can create your own exception class, derived from Exception or from a subclass of Exception , such as IOException .

In Listing 17.4, CircleWithException.java, the setRadius method throws an exception if the radius is negative. Suppose you wish to pass the radius to the handler. In that case, you may create a custom exception class, as shown in Listing 17.8.

Listing 17.8. InvalidRadiusException.java
 1   public class    InvalidRadiusException   extends   Exception  {  2   private double   radius;  3  4  /** Construct an exception */  5    public   InvalidRadiusException(   double   radius)  {  6   super   (   "Invalid radius "   + radius);  7   this   .radius = radius;  8    }  9 10  /** Return the radius */  11   public double   getRadius() { 12   return   radius; 13    } 14  } 

This custom exception class extends java.lang.Exception (line 1). The Exception class extends java.lang.Throwable . All the methods (e.g., getMessage() , toString() , and printStackTrace() ) in Exception are inherited from Throwable . The Exception class contains four constructors. Among them, the following two constructors are often used:

  • public Exception() Constructs an exception with no message.


    [Page 593]
  • public Exception(String message) Constructs an exception with the specified message.

Line 6 invokes the superclass's constructor with a message. This message will be set in the exception object and can be obtained by invoking getMessage() on the object.

Tip

Most exception classes in the Java API contain two constructors: a no-arg constructor and a constructor with a message parameter.


To create an InvalidRadiusException , you have to pass a radius. So the setRadius method in Listing 17.4 can be modified as follows :

  /** Set a new radius */    public void   setRadius(   double   newRadius)    throws   InvalidRadiusException  {   if   (newRadius >=     )     radius = newRadius;   else      throw new   InvalidRadiusException(newRadius);  } 

The following code creates a circle object and sets its radius to -5 :

   try   {   CircleWithException1 c =   new   CircleWithException1(   4   );   c.setRadius(   -5   ); }   catch   (  InvalidRadiusException ex  ) {   System.out.println(   "The invalid radius is "   + ex.getRadius()); } 

Invoking setRadius(-5) throws an InvalidRadiusException , which is caught by the handler. The handler displays the radius in the exception object ex .

Tip

Can you declare a custom exception class by extending RuntimeException ? Yes, but it is not good, because it makes your custom exception unchecked. It is better to make a custom exception checked so that the complier can force these exceptions to be caught in your program.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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