Java FAQ's
Java FAQ's
User-defined exceptions may be implemented by
1 defining a class to respond to the exception and
2 embedding a throw statement in the try block where the exception can occur or
declaring that the method throws the exception (to another method where it is
The developer can define a new exception by deriving it from the Exception class as follows:
public class MyException extends Exception {
/* class definition of constructors (but NOT the exception handling code) goes here public MyException() {
public MyException( String errorMessage ) {
super( errorMessage );
The throw statement is used to signal the occurance of the exception within a try block. Often, exceptions are instantiated in the same statement in which they are thrown using the
throw new MyException("I threw my own exception.")
To handle the exception within the method where it is thrown, a catch statement that handles MyException, must follow the try block. If the developer does not want to handle the exception in the method itself, the method must pass the exception using the syntax:
public myMethodName() throws MyException