Thursday, January 30, 2014

Creating your own exception class in java

Source:
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

Blog Archive