Exception handling


Published on

Description about Exception Handling

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • The first kind of exception is the checked exception . These are exceptional conditions that a well-written application should anticipate and recover from. The second kind of exception is the error . These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. Errors are not subject to the Catch or Specify Requirement. Errors are those exceptions indicated by Error and its subclasses The third kind of exception is the runtime exception . These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API.
  • If you feel that a method doesn't know how to handle a particular error, you can throw an exception from the method and let someone else deal with it. If you throw a "checked" exception, you enlist the help of the Java compiler to force client programmers to deal with the potential exception, either by catching it or declaring it in the throws clause of their methods.
  • Exception handling

    1. 1. Exception Handling Presentation on Presented By : Sumit Lole
    2. 2. Agenda <ul><li>What is Exception? </li></ul><ul><li>Exception Hierarchy </li></ul><ul><li>Exception vs. Error </li></ul><ul><li>Types of Exception </li></ul><ul><li>Default Exception Handler </li></ul><ul><li>Exception Handling </li></ul><ul><li>Creating Your own Exception </li></ul>
    3. 3. Exception <ul><li>Definition:   </li></ul><ul><li>“ An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions” </li></ul><ul><li>An exception occurs when our code asks the JVM to perform the technically impossible and unanticipated (by the compiler)! </li></ul>
    4. 4. Exception Hierarchy <ul><li>All exception types are subclasses of the built-in class Throwable </li></ul><ul><li>Throwable has two subclasses : </li></ul><ul><ul><li>Exception </li></ul></ul><ul><ul><li>Error </li></ul></ul>
    5. 5. Throwable Error Exception Runtime Exception IO Exception The Java Exception Hierarchy
    6. 6. Error <ul><li>Error hierarchy describes internal errors and resource exhaustion. </li></ul><ul><li>Don’t throw an object of this type! </li></ul><ul><li>Relatively rare </li></ul><ul><li>Mostly beyond programmers control </li></ul>
    7. 7. Types of Exceptions in Java <ul><li>Checked exceptions </li></ul><ul><ul><li>All exception inherited from Exception Class </li></ul></ul><ul><li>Unchecked exceptions </li></ul><ul><ul><li>All exception inherited from RuntimeException Class </li></ul></ul>
    8. 8.
    9. 9. Default Exception Handler ? <ul><li>Default handler prints a stack trace from the point at which the exception occurred, and terminates the program </li></ul><ul><li>Example: BasicDemo.java </li></ul>
    10. 10. Exception Handling <ul><li>Anticipate the error by the user/system. </li></ul><ul><li>Return the program to a safe state that enables the user to execute other commands. </li></ul><ul><li>Inform the user of the error’s cause. </li></ul><ul><li>Allow the user to save work and terminate the program gracefully. </li></ul>
    11. 11. Ways to throw an Exception <ul><li>Calling a method that throws a checked exception </li></ul><ul><li>Code detects an error and generates checked exception with throw statement </li></ul><ul><li>Programming error e.g. a[-1] =0; generates </li></ul><ul><li>an unchecked exception ArrayIndexOutOfBoundsException </li></ul><ul><li>4. JVM or runtime library internal error </li></ul>
    12. 12. Keywords for Java Exceptions <ul><li>throws </li></ul><ul><li>throw </li></ul><ul><li>try </li></ul><ul><li>catch </li></ul><ul><li>finally </li></ul>
    13. 13. Catching Exceptions <ul><li>“ What goes up must come down” </li></ul><ul><li>Catching exceptions is a bit trickier </li></ul><ul><li>An uncaught exception will terminate the program … </li></ul><ul><li>… print the exception type to screen … </li></ul><ul><li>… print the stack trace. </li></ul>
    14. 14. Using try and catch <ul><li>Placed the code (which is suppose to throw an exception) inside a block of code starting with the “ try ” keyword </li></ul><ul><li>The catch clause should follow immediately the try block. </li></ul><ul><li>Once the catch statement has executed, program control continues with the next line in the program following the entire try/catch mechanism </li></ul><ul><li>Example : InitialDemo.java </li></ul>
    15. 15. Nested Try Statement <ul><li>A try statement can be inside the block of another try </li></ul><ul><li>Each time a try statement is entered, the context of that exception is pushed on the stack </li></ul><ul><li>If an inner try statement does not have a catch, then the next try statement’s catch handlers are inspected for a match </li></ul><ul><li>If a method call within a try block has try block within it, then then it is still nested try </li></ul><ul><li>Example : ExceptionChaining.java </li></ul>
    16. 16. What information does java.lang.Exception contain <ul><li>The type of exception -- the exception class </li></ul><ul><li>Where the exception occurred -- the stack trace </li></ul><ul><li>Context and explanatory information -- the error message, and other state information </li></ul><ul><li>Example : StackTrace.java </li></ul>
    17. 17. finally <ul><li>It is used to handle premature execution of a method (i.e. a method open a file upon entry and closes it upon exit) </li></ul><ul><li>finally creates a block of code that will be executed after try/catch block has completed and before the code following the try/catch block </li></ul><ul><li>finally clause will execute whether or not an exception is thrown </li></ul>
    18. 18. Some Important Exception that generally occurs… <ul><li>IOException </li></ul><ul><li>ClassNotFoundException </li></ul><ul><li>RuntimeException </li></ul><ul><ul><li>Most occurring exception </li></ul></ul><ul><ul><li>Caused by programming error </li></ul></ul><ul><ul><li>“ If it’s a RuntimeException it’s your fault!” </li></ul></ul><ul><ul><li>Example : </li></ul></ul><ul><ul><ul><ul><li>ArrayIndexOutOfBoundException </li></ul></ul></ul></ul><ul><ul><ul><ul><li>IndexOutOfBound </li></ul></ul></ul></ul><ul><ul><ul><ul><li>ArithmeticException </li></ul></ul></ul></ul><ul><ul><ul><ul><li>NullPointerException </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Example : ExceptionExample.java </li></ul></ul></ul></ul>
    19. 19. throw <ul><li>It is possible for your program to throw an exception explicitly </li></ul><ul><li>throw ExceptionInstance </li></ul><ul><li>Here, ExceptionInstance must be an object of type Throwable or a subclass Throwable </li></ul><ul><li>There are two ways to obtain a Throwable objects: </li></ul><ul><ul><li>Using a parameter into a catch clause </li></ul></ul><ul><ul><li>Creating one with the new operator </li></ul></ul>
    20. 20. Creating Your Own Exceptions <ul><li>Java provides over 40 categories of Exceptions </li></ul><ul><li>Java allows you to create your own Exceptions </li></ul><ul><ul><li>Extend a subclass of Throwable </li></ul></ul><ul><li>Exception class constructors </li></ul><ul><ul><li>Exception() </li></ul></ul><ul><ul><li>Exception(String message) </li></ul></ul><ul><ul><li>Exception(String message, Throwable cause) </li></ul></ul><ul><ul><li>Exception(Throwable cause) </li></ul></ul>
    21. 21. Example <ul><li>throw statement : </li></ul><ul><li>throw <object reference expression>; </li></ul><ul><li>Class DivisionByZeroException extends Exception{ </li></ul><ul><li>DivisionByZeroException(String msg) { </li></ul><ul><li>super(msg); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>public void division(){ </li></ul><ul><li>int num1 = 10; </li></ul><ul><li>int num2 = 0; </li></ul><ul><li>if(num2 == 0) </li></ul><ul><li>throw new DivisionByZeroException(); </li></ul><ul><li>System.out.println(num1/num2); </li></ul><ul><li>} </li></ul>
    22. 22. throws <ul><li>If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception </li></ul><ul><ul><li>type method-name parameter-list) throws exception-list </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>// body of method </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>It is not applicable for Error or RuntimeException, or any of their subclasses </li></ul>
    23. 23. Rule for Method Overriding <ul><li>While overriding, the new method definition in the subclass can only specify all or a subset of the exception classes specified in the throws clause of the overridden method in the superclass </li></ul>
    24. 24. Some Methods Inherited from Throwable <ul><li>fillStackTrace() returns Throwable </li></ul><ul><li>getCause() returns Throwable </li></ul><ul><li>getMessage() returns String </li></ul><ul><li>getStackTrace() return StackTraceElement[] - Throwable </li></ul><ul><li>initCause(Throwable) </li></ul><ul><li>printSatckTrace() return void </li></ul><ul><li>setStackTrace(Throwable) </li></ul><ul><li>toString() return String </li></ul>
    25. 25. Queries