Java |
|||||||||||
Exception Handling
Why We need Exception Handling ?
C:\temp> java Exception01
Exception in thread "main" java.lang.ArithmeticException: / by zero at Exception01.main(Exception01.java:5)
NOTE : Program crashed and 'System.out.println("Reached the end of the program");' is not executed.
How to Capture the Exceptions ?
C:\temp> java Exception02
An Exception occurred with following details : java.lang.ArithmeticException: / by zero <-- System.out.println(" " + e); java.lang.ArithmeticException: / by zero <-- System.out.println(" " + e.toString()); java.lang.ArithmeticException: / by zero <-- e.printStackTrace(); at Exception02.main(Exception02.java:8) class java.lang.ArithmeticException <-- System.out.println(" " + e.getClass()); / by zero <-- Reached the end of the program <-- System.out.println("Reached the end of the program");
NOTE : Exception occurred but program move forward
How to Capture a Specific Exception ?
C:\temp> java Exception03
An ArithmeticException occurred with following details : java.lang.ArithmeticException: / by zero <-- System.out.println(" " + e); java.lang.ArithmeticException: / by zero <-- System.out.println(" " + e.toString()); java.lang.ArithmeticException: / by zero <-- e.printStackTrace(); at Exception03.main(Exception03.java:8) class java.lang.ArithmeticException <-- System.out.println(" " + e.getClass()); / by zero <-- System.out.println(" " + e.getMessage)); Reached the end of the program <-- System.out.println("Reached the end of the program");
NOTE : Exception occurred but program move forward
How to Capture a Multiple Exceptions ?
C:\temp> java Exception04
An ArrayIndexOutOfBoundsException occurred with following details : java.lang.ArrayIndexOutOfBoundsException: 5 <-- System.out.println(" " + e); java.lang.ArrayIndexOutOfBoundsException: 5 <-- System.out.println(" " + e.toString()); java.lang.ArrayIndexOutOfBoundsException: 5 <-- e.printStackTrace(); at Exception04.main(Exception04.java:12) class java.lang.ArrayIndexOutOfBoundsException <-- System.out.println(" " + e.getClass()); 5 <-- System.out.println(" " + e.getMessage()); Reached the end of the program <-- System.out.println("Reached the end of the program");
NOTE : Exception occurred but program move forward
C:\temp> java Exception05
An Exception occurred with following details : java.io.FileNotFoundException: input.txt (The system cannot find the file specified) <-- System.out.println(" " + e); java.io.FileNotFoundException: input.txt (The system cannot find the file specified) <-- System.out.println(" " + e.toString()); java.io.FileNotFoundException: input.txt (The system cannot find the file specified) <-- e.printStackTrace(); at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at Exception05.main(Exception05.java:11) java.io.FileNotFoundException <-- System.out.println(" " + e.getClass()); input.txt (The system cannot find the file specified) <-- System.out.println(" " + e.getMessage()); Reached the end of the program <-- System.out.println("Reached the end of the program");
NOTE : Exception occurred but program move forward
|
|||||||||||