Java Reference
In-Depth Information
Listing 9-8. A MyException Class That Extends the Exception Class
// MyException.java
package com.jdojo.exception;
public class MyException extends Exception {
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(Throwable cause) {
super(cause);
}
}
The first constructor creates an exception with null as its detailed message. The second constructor creates an
exception with a detailed message. The third and fourth constructors let you create an exception by wrapping another
exception with/without a detailed message.
You can throw an exception of type MyException .
throw new MyException("Your message goes here");
You can use the MyException class in a throws clause in a method/constructor declaration or as a parameter type
in a catch block.
import com.jdojo.exception.MyException;
...
public void m1() throws MyException {
// Code for m1() body goes here
}
try {
// Code for the try block goes here
}
catch(MyException e) {
// Code for the catch block goes here
}
Table 9-1 shows some of the commonly used methods of the Throwable class. Note that the Throwable
class is the superclass of all exception classes in Java. All of the methods shown in this table are available in all
exception classes.
 
Search WWH ::




Custom Search