Exceptions & Its Handling

2,087 views
2,014 views

Published on

Published in: Technology, News & Politics
1 Comment
0 Likes
Statistics
Notes
  • pls can u get me this presentation
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total views
2,087
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
101
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Exceptions & Its Handling

  1. 1. Exceptions & its Handling
  2. 2. What’s this Exception <ul><li>It’s an event, which can occur during the execution of a program, & due to which the normal flow of the program's instructions may be disrupted. </li></ul>Class Test { public static void main(String[] args) { System.out.println( 6/0 ); System.out.println( “Hello” ); // do something else } } Exception in thread &quot;main&quot; java.lang.ArithmeticException: / by zero at NewClass.main(NewClass.java:20)
  3. 3. Throwing an Exception <ul><li>When an error occurs within a method, the run-time system creates an object and hands it off to the method. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the relevant method is called throwing an exception. </li></ul><ul><li>After a method throws an exception, the runtime system attempts to find the ordered list of methods that had been called to get to the method where the error occurred. The list of methods is known as the call stack </li></ul>
  4. 4. Understanding the Call Stack <ul><li>Exception in thread &quot;main&quot; java.lang.ArithmeticException: / by zero </li></ul><ul><li>at NewClass.y(NewClass.java:23) </li></ul><ul><li>at NewClass.x(NewClass.java:21) </li></ul><ul><li>at NewClass.main(NewClass.java:19) </li></ul>y(), where exception occured x() main() Class Test { public static void main(String[] args) { x(); } void x() { y(); } void y() { int a = 7/0; } }
  5. 5. Exception Handler <ul><li>The runtime system searches the call stack for a method that contains a block of code that can handle the exception. This block of code is called an exception handler. </li></ul><ul><li>The search begins with the method in which the error occurred and proceeds through the call stack in the reverse order in which the methods were called. </li></ul><ul><li>When an appropriate handler is found, the runtime system passes the exception object to the handler. </li></ul><ul><li>An exception handler is considered appropriate if the type of the exception object thrown matches the type that can be handled by the handler. </li></ul>
  6. 6. Searching for the Exception Handler in the call stack <ul><li>If main forwarded the exception, the thread will abnormally terminate. </li></ul>y(), where exception occured x() main() throws exception should handle but may forward exception Looking for appropriate handler Looking for appropriate handler should handle but may forward exception
  7. 7. Catch or Specify Requirement <ul><li>The code that might throw certain exception must be enclosed either by : </li></ul><ul><ul><ul><li>A try statement that catches the exception. The try must provide a handler for the exception. </li></ul></ul></ul><ul><ul><ul><li>A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception. </li></ul></ul></ul><ul><li>Code that fails to honor the Catch or Specify requirement will not compile. </li></ul><ul><li>Not all exceptions are subject to the Catch or Specify requirement. </li></ul>
  8. 8. Exception Types : 3 <ul><li>Checked Exception </li></ul><ul><li>Run-time Exception </li></ul><ul><li>Error </li></ul>UnChecked Exception
  9. 9. Class Hierarchy java.lang.Object java.lang.Throwable java.lang.Error java.lang.Exception java.lang.RuntimeException
  10. 10. Checked Exception <ul><li>An exceptional condition that a well-written application should anticipate and recover from. </li></ul><ul><ul><ul><li>java.io.FileNotFoundException </li></ul></ul></ul><ul><ul><ul><li>Java.io.IOException </li></ul></ul></ul><ul><ul><ul><li>Java.io.InterruptedException </li></ul></ul></ul><ul><li>Checked exceptions are subject to the Catch or Specify Requirement. </li></ul><ul><li>All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses. </li></ul>
  11. 11. Runtime Exception <ul><li>Exceptional conditions that are internal to the application, and that the application usually cannot anticipate. </li></ul><ul><li>Usually indicate programming bugs, such as logic errors or improper use of an API. </li></ul><ul><li>Runtime exceptions are not subject to the Catch or Specify requirement. Runtime exceptions are those indicated by RuntimeException and its subclasses. </li></ul><ul><li>Ex :- </li></ul><ul><ul><ul><li>java.lang.NullPointerException </li></ul></ul></ul><ul><ul><ul><li>java.lang.ArrayIndexOutOfBoundsException </li></ul></ul></ul><ul><ul><ul><li>java.lang.ArithmeticException </li></ul></ul></ul>
  12. 12. Error <ul><li>Exceptional conditions that are external to the application, and that the application usually cannot anticipate. </li></ul><ul><li>Ex :- java.io.IOError </li></ul><ul><li>Errors are not subject to the Catch or Specify Requirement. </li></ul><ul><li>Errors are those exceptions indicated by Error and its subclasses. </li></ul>
  13. 13. Runtime Exception due to Logical Errors public class NewClass { public static void main(String[] args) { int array[] = {6,7,8,9,10}; for(int index=0; index<array.length+2; index++) System.out.print(array[index]+” “); System.out.println(&quot;After loop&quot;); } } 6 7 8 9 10 Exception in thread &quot;main&quot;java.lang.ArrayIndexOutOfBoundsException: 5 at NewClass.main(NewClass.java:22)
  14. 14. Runtime Exception due to Logical Errors <ul><li>public class NewClass </li></ul><ul><li>{ </li></ul><ul><li> static Integer i; </li></ul><ul><li> public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;1st statement&quot;); </li></ul><ul><li>System.out.println(i.intValue()); </li></ul><ul><li>System.out.println(&quot;3rd Statement&quot;); </li></ul><ul><li>show(&quot;tom&quot;); </li></ul><ul><li>} </li></ul><ul><li>static void show(String msg) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(msg.toUpperCase()); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>1st statement Exception in thread &quot;main&quot; java.lang.NullPointerException at NewClass.main(NewClass.java:21)
  15. 15. Runtime Exception due to improper use of API <ul><li>class Test </li></ul><ul><li>{ </li></ul><ul><ul><ul><li>static Thread t2; </li></ul></ul></ul><ul><ul><ul><li>public static void main(String[] args) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>Thread t1 = new Thread() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>public void run() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>for(int i=0; i<10; i++) </li></ul></ul></ul><ul><ul><ul><li>System.out.print(“ new thread ”); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>}; </li></ul></ul></ul><ul><ul><ul><li>t1.start(); </li></ul></ul></ul><ul><ul><ul><li>t2.start(); </li></ul></ul></ul><ul><ul><ul><li>for(int i=0; i<5; i++) </li></ul></ul></ul><ul><ul><ul><li>System.out.print(“ Main thread ”); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>} </li></ul>Exception in thread &quot;main&quot; java.lang.NullPointerException at NewClass.main(NewClass.java:30) new thread new thread new thread new thread new thread new thread new thread new thread new thread new thread
  16. 16. Runtime Exception due to improper use of API <ul><li>class Test </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(“First statement”); </li></ul><ul><li>m(“india”); </li></ul><ul><li>m(35); </li></ul><ul><li>System.out.println(“Last statement”); </li></ul><ul><li>} </li></ul><ul><li>static void m(Object o) </li></ul><ul><li>{ </li></ul><ul><li>Integer i = (Integer)o; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>First statement Exception in thread &quot;main&quot; java.lang.ClassCastException: java.lang.String at Test.m(NewClass.java:27) at Test.main(NewClass.java:21)
  17. 17. Dealing with Exception <ul><li>Using try & catch block of code. </li></ul>public class NewClass { public static void main(String[] args) { int array[] = {6,7,8,9,10}; try{ for(int index=0; index<array.length+2; index++) } catch(Exception e) { System.out.println(“Element with this index doen’t exist”); } System.out.print(array[index]+” “); System.out.println(&quot;After loop&quot;); } }
  18. 18. <ul><li> try{ </li></ul><ul><li> System.out.println(9/0); </li></ul><ul><li> System.out.println(&quot;Hello&quot;); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e) </li></ul><ul><li>{ </li></ul><ul><li> System.out.println(e.getMessage()); </li></ul><ul><li> return; </li></ul><ul><li>} </li></ul><ul><li> System.out.println(&quot;Last statement&quot;); </li></ul>
  19. 19. Using finally block <ul><li>try{ </li></ul><ul><li>System.out.println(9/0); </li></ul><ul><li>System.out.println(&quot;Hello&quot;); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e) </li></ul><ul><li>{ </li></ul><ul><li> System.out.println(e.getMessage()); </li></ul><ul><li> return; </li></ul><ul><li>} </li></ul><ul><li>finally{System.out.println(&quot;2nd Last statement&quot;);} </li></ul><ul><li>System.out.println(&quot;Last statement&quot;); </li></ul>
  20. 20. java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException IndexOutOfBoundsException ArrayIndexOutOfBoundsException StringIndexOutOfBoundsException
  21. 21. <ul><li>int arr[] = {5,6,7}; </li></ul><ul><li>try{ </li></ul><ul><li>System.out.println(arr[5]); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;first catch&quot;); </li></ul><ul><li>} </li></ul><ul><li>catch(RuntimeException re) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;2nd catch&quot;); </li></ul><ul><li>} </li></ul>
  22. 22. <ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(m()); </li></ul><ul><li>} </li></ul><ul><li>static int m() </li></ul><ul><li>{ </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>int x = 7/0; </li></ul><ul><li>return x; </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;catch block&quot;); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>finally </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;finally block&quot;); </li></ul><ul><li>return 1; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  23. 23. Handling Checked Exceptions <ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>m(); </li></ul><ul><li>} </li></ul><ul><li>static void m() </li></ul><ul><li>{ </li></ul><ul><li> FileInputStream fis = new FileInputStream(&quot;abc.java&quot;); </li></ul><ul><li>} </li></ul>try{ } catch(FileNotFoundException e) { //….. } throws FileNotFoundException try{ } catch(IOException e) { //….. } throws FileNotFoundException
  24. 24. Creating Custom UnChecked Exceptions public class NewClass { public static void main(String[] args) { m(); } static void m() { throw new MyException(); } } class MyException extends RuntimeException { MyException() { super(&quot;This is my exception&quot;); } } Exception in thread &quot;main&quot; Pack.MyException: This is my exception at NewClass.m(NewClass.java:24) at NewClass.main(NewClass.java:20)
  25. 25. Creating Custom Checked Exceptions <ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>m(); </li></ul><ul><li>} </li></ul><ul><li>static void m() </li></ul><ul><li>{ </li></ul><ul><li>throw new MyException(); </li></ul><ul><li>} </li></ul><ul><li>class MyException extends RException </li></ul><ul><li>{ </li></ul><ul><li>MyException() </li></ul><ul><li>{ </li></ul><ul><li>super(&quot;This is my exception&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>

×