Académique Documents
Professionnel Documents
Culture Documents
82
Ex no: HANDLING USER DEFINED EXCEPTIONS Date: AIM : To write a java program by using the exception handling with user defined exception method. ALGORITHM: 1. Start the program. 2. Import all the content of exception from java.lang package. 3. Derive the class from the base class using extends keyword. 4. Using the super keyword the private member is inherited. 5. Declare and define the testmyexception class. 6. Declare and define the void main function. 7. Declare the instant variable to store the values. 8. In the try block the calculation is done. 9. By using if statement checks the condition for the value of z. 10. By using the throw function the caughted exception is passed to the catch block. 11. In the catch block if the exception is caughted print the exception. 12. In finally block the statement is printed to point the exception is ended. 13. Stop the program.
Page No.
83
PROGRAM: import java.lang.Exception; class myexception extends Exception { myexception(String message) { super(message); } } class testmyexception { public static void main(String args[]) { int x=5,y=1000; try { float z=(float)x/(float)y; if(z<0.01) { throw new myexception("number is too small"); } } catch(myexception e) { System.out.println("caught myexception"); System.out.println(e); } finally { System.out.println("iam always here"); } } }
Page No.
84
RESULT: Thus the java program for the user defined exception has been executed and verified successfully.