You are on page 1of 2

Creating user defined Exception User exception class as a sub class to exception class Ex: - class MyException extends

Exception Write a default constructor in user exception class Ex MyException( ) { } Write a string parameterized constructor & from their call the super class constructor. Ex: - MyException(String str) { Super(str); } To raise the exception creates user exception class object & throw it using throw statement. Ex: - throw me; // user defined exception class MyException extends Exception { private static int accno[]={12,13,11,14,15}; private static String name[]={"sun","Sud","sree","siri","latha"}; private static double bal[]={345.53,5345.45,534.534,5435.56,324.643}; MyException( ) { } MyException(String str) { super(str); } public static void main(String args[]) { try { System.out.println("accno "+"\t"+"name"+ "balance "); for(int i=0;i<5;i++) { System.out.println(accno[i]+"\t"+name[i]+"\t"+bal[i]); if(bal[i]<340.00) { MyException me=new MyException("balance ammount is less"); throw me; } } } catch(MyException me) { me.printStackTrace( );

} } } D:\psr\Core java>javac MyException.java D:\psr\Core java>java MyException accno namebalance 12 sun 345.53 13 Sud 5345.45 11 sree 534.534 14 siri 5435.56 15 latha 324.643 MyException: balance ammount is less at MyException.main(MyException.java:24)

You might also like