Java Reference
In-Depth Information
RuntimeException should end with Exception . Also, quite a number of standard
Exception classes are available on the Internet, so always be sure to check if an
Exception class exists before making your own.
This exception will be used when no database is available for the application to
connect to:
class NoDatabaseAvailableException extends IOException {
...
}
By convention, Exception classes define a constructor that accepts a String para-
meter. This allows the class that created the exception to place some contextual in-
formation in the Exception . In addition, Exception classes normally support a
method named getMessage() . This method returns a string that contains the contex-
tual information and perhaps more detailed information about the error condition.
class NoDatabaseAvailableException extends IOException {
public NoDatabaseAvailableException (String message) {
super (message);
}
...
public String getMessage () {
return "Unable to access the database for" +
super.getMessage ();
}
}
Your new exception is now ready for use.
You can envision an Exception object as an optional return value from a
method. You already know that methods can return values as defined in the
method interface. In addition, a method can create an Exception object and return
this object instead of the normal value.
A Java method that may create an exception must publish that property as part
of its interface definition. The syntax for this is as follows:
Search WWH ::




Custom Search