Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages.

Similar presentations


Presentation on theme: "1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages."— Presentation transcript:

1 1

2 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages that do not support exception handling, errors must be checked and handled manually. - Through the error codes But java brings run time error management in to the object oriented world.

3 3 Example 1:... class Example1_Exceptions { public static void main(String arg[]) { int arr[]={3,5,6,9,10}; for(int i=0;i<6;i++) { System.out.println(arr[i]); } class Example1_Exceptions { public static void main(String arg[]) { int arr[]={3,5,6,9,10}; for(int i=0;i<6;i++) { System.out.println(arr[i]); } java.lang.ArrayIndexOutOfBoundsException

4 4 Example 2 :... class Student { int age; String name; } class Example2_Exceptions { public static void main(String arg[]) { Student s[]=new Student[5]; s[0].age=21; s[0].name="Kamal“; } class Student { int age; String name; } class Example2_Exceptions { public static void main(String arg[]) { Student s[]=new Student[5]; s[0].age=21; s[0].name="Kamal“; } java.lang.NullPointerException

5 5 Example 3 :... class Example3_Exceptions { public static void main(String arg[]) { int a=0; float avg=56/a; System.out.println(avg); } class Example3_Exceptions { public static void main(String arg[]) { int a=0; float avg=56/a; System.out.println(avg); } java.lang.ArithmeticException: / by zero

6 04/30/106 Example 4 :... class Example3_Exceptions { public static void main(String arg[]) { String weight="34kg"; double w=Double.parseDouble(weight); System.out.println(w); } class Example3_Exceptions { public static void main(String arg[]) { String weight="34kg"; double w=Double.parseDouble(weight); System.out.println(w); } Exception in thread "main" java.lang.NumberFormatException: For input string: "34kg“ at sun.misc.FloatingDecimal.readJavaFormatString( FloatingDecimal.java:1224) at java.lang.Double.parseDouble(Double.java:510) at ExceptionHandling.main(ExceptionHandling.java:12)

7 7 Exception Hierarchy... Java.lang.Throwable J ava.lang.Exception Java.lang.Error Java.lang.RuntimeException Java.lang.IOException

8 8 How java handles the exceptions ?... When the java run time system detects a run time error in the program, it constructs a new exception object and then throws this exception. This causes the execution of the program to stop at the point of detecting the error. Because once an exception has been thrown, it catches by an exception handler and deals with it immediately. This default handler is provided by the java runtime system. These exceptions are called as uncaught exceptions. The default handler displays a String describing the exception specifying the point at which the exception occurred. Then terminates the program.

9 9 Managing Exceptions... Managing exceptions within the code itself is very important, although the default handler manages the exceptions into some extent. 1.It allows to fix the error 2.It prevents the program from automatically terminating. Java Exceptions are managed via Five keywords. I.try II.catch III.finally IV.throw V.throws

10 10 Using try and catch... To handle a runtime error, simply enclose the code that wants to monitor, inside a try block. Immediately following the try block, include a catch clause that specifies the exception type that wishes to catch. try { //Code to monitor an error } catch(Exception_Type object_of_the_instance) { //Task to do when an exception is found } try { //Code to monitor an error } catch(Exception_Type object_of_the_instance) { //Task to do when an exception is found }

11 04/30/1011 try-catch Example (1):... class Example_Exceptions { public static void main(String arg[]) { try { int a=0; float avg=56/a; System.out.println(avg); } catch(ArithmeticException err) { System.out.println(“Devides by zero”); } class Example_Exceptions { public static void main(String arg[]) { try { int a=0; float avg=56/a; System.out.println(avg); } catch(ArithmeticException err) { System.out.println(“Devides by zero”); }

12 12 Using try and catch (cont)... Once an exception is thrown, program control transfers out of the try block into the catch block. Execution never returns to the try block from a catch. Once the catch statement has executed, program control continues with the next line in the program following the entire try-catch block. To print the error message as it’s original format, catch(Exception err) { System.out.println(err.toString()); }

13 04/30/1013 try-catch Example (2):... Import java.util.Random; class Example_Exceptions { public static void main(String arg[]) { try { Random r=new Random(); int b=r.nextInt(); int c=r.nextInt(); int a=b/c; System.out.println(“b: ”+b+”\nc: ”+c+”\na:”+a); } catch(ArithmeticException e) { System.out.println(“Divides by zero”); } Import java.util.Random; class Example_Exceptions { public static void main(String arg[]) { try { Random r=new Random(); int b=r.nextInt(); int c=r.nextInt(); int a=b/c; System.out.println(“b: ”+b+”\nc: ”+c+”\na:”+a); } catch(ArithmeticException e) { System.out.println(“Divides by zero”); }

14 04/30/1014 Using try, catch and finally... Suppose there is an action that absolutely must do, regardless of an exception is thrown or not. In such cases, the finally clause is essential. try { //Code to monitor an error } catch(Exception_Type object_of_the_instance) { //Task to do when an exception is found } finally { //The task to do regardless of exception occurs or not } try { //Code to monitor an error } catch(Exception_Type object_of_the_instance) { //Task to do when an exception is found } finally { //The task to do regardless of exception occurs or not }

15 15 Using try and finally... It is possible to use only the try and finally clauses without using catch clause. try { //Code to monitor an error } finally { //The task to do regardless of exception occurs or not } try { //Code to monitor an error } finally { //The task to do regardless of exception occurs or not }

16 04/30/1016 try, catch and finally-Example(1)... Reading a text file. try { //Open the text file //Read the text file -----------------IOException may occur when reading the text file //Close the text file } catch(IOException err) { //Task to do when an exception is found } finally { //Close the text file } try { //Open the text file //Read the text file -----------------IOException may occur when reading the text file //Close the text file } catch(IOException err) { //Task to do when an exception is found } finally { //Close the text file }

17 17 Using throws... If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature. class Checked_Exceptions { ( method signature) throws Exception_Type { //The code that will cause to a checked exception } class Checked_Exceptions { ( method signature) throws Exception_Type { //The code that will cause to a checked exception }

18 18 throws Example... class Checked_Exceptions { public static void main(String args[]) throws IOException { //open the file to read //Read the file //Close the file } class Checked_Exceptions { public static void main(String args[]) throws IOException { //open the file to read //Read the file //Close the file }

19 19 Using throws (cont)... A method may return more than one exception, in which case the exceptions are declared in a list separated by commas. class Checked_Exceptions { ( method signature) throws Exception1, Exception2…….. { //The code that will cause to a checked exception } class Checked_Exceptions { ( method signature) throws Exception1, Exception2…….. { //The code that will cause to a checked exception }

20 20 Using throw... Class Throw_Exceptions { ( method signature) { try { throw new NullPointerException(“Exception occurred”); } catch(NullPointerException err) { System.out.println(err.toString ()); } Class Throw_Exceptions { ( method signature) { try { throw new NullPointerException(“Exception occurred”); } catch(NullPointerException err) { System.out.println(err.toString ()); }

21 21 NOTE on Exceptions... DO NOT USE EXCEPTION HANDLING, If the exception could be handled easily with a simple expression. Ex: ArrayIndexOutOfBoundsException could easily handled using correct length of the array. ArithmeticExceptions like divide by zero, could be handled using simple if-else statements. if(a==0) { }

22 22 NOTE on Exceptions (cont)... Exceptions take up a lot of processing time for the java program. a simple test or series of tests will run much faster than exception handling and make the java program more efficient. EXCEPTIONS SHOULD BE USED ONLY FOR TRULY EXCEPTIONAL CASES THAT ARE OUT OF CONTROL. EXCEPTIONS SHOULD BE USED ONLY FOR TRULY EXCEPTIONAL CASES THAT ARE OUT OF CONTROL.


Download ppt "1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages."

Similar presentations


Ads by Google