Back To Java Exceptions Index
//program to create your own exception class MyException extends Exception { public MyException(String s) { // Call constructor of parent Exception super(s); } } // A Class that uses above MyException public class Main1 { // Driver Program public static void main(String args[]) { try { // Throw an object of user defined exception throw new MyException("My Exception Demo"); } catch (MyException ex) { System.out.println("Caught"); // Print the message from MyException object System.out.println(ex.getMessage()); } } }