javacodex.com
Java Examples
Java Examples
memu home questions

Create Your Own Exception Class

The following java example demonstrates how to create your own exception class.

Source: (CreateException.java)

class MyException extends Exception {
   public MyException() { }
   public MyException(String msg) {
      super(msg);
   }
};
 
public class CreateException {
   public static void main(String[] args) {
      try {
         throw new MyException("my message");
      } catch (Exception e) {
         System.out.println("Exception Type: "  + e );
      }
   }
}
 

Output:

# java CreateException 
Exception Type: MyException: my message

Contact: javacodex@yahoo.com