Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Exception handling

224 views

Published on

introduc

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

  • Be the first to like this

Exception handling

  1. 1. Exception Handling
  2. 2. What is exception ? ● An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. ● Anything that occurs in an unexpected way is called as exception
  3. 3. Error ● These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. ● Errors are typically ignored in your code because you can rarely do anything about an error.
  4. 4. Difference between Error and Exception ● An Error "indicates serious problems that a reasonable application should not try to catch.” ● An Exception "indicates conditions that a reasonable application might want to catch."
  5. 5. Why exception occurs An exception can occur for many different reasons, including the following: ● A user has entered invalid data. ● A file that needs to be opened cannot be found. ● A network connection has been lost in the middle of communications or the JVM has run out of memory. ●
  6. 6. Types of Exceptions ● Checked exceptions ● Runtime exceptions
  7. 7. Checked Exception ● A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. ● For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.
  8. 8. Runtime Exception ● A runtime exception is an exception that occurs that probably could have been avoided by the programmer. ● As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.
  9. 9. Categories of exception ● In Java, it is possible to define two catergories of Exceptions and Errors. ● JVM Exceptions: - These are exceptions/errors that are exclusively or logically thrown by the JVM. ● Examples : NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, ● Programmatic exceptions: - These exceptions are thrown explicitly by the application or the API programmers ● Examples: IllegalArgumentException, IllegalStateException.
  10. 10. Advantages of exception handling ● Separating Error-Handling Code from "Regular" Code ● Grouping and Differentiating Error Types
  11. 11. Keywords to handle exceptions Exception Handling contains 5 keywords ● 1. try ● 2. catch ● 3. finally ● 4. throws ● 5. throw
  12. 12. Syntax for Exception handling try {} catch(Exception e){ } Finally{ }
  13. 13. Purpose of try, catch and finally ● The try block identifies a block of code in which an exception can occur. ● The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception. ● The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block. ●
  14. 14. Exception Hierarchy
  15. 15. Example of Exception handling
  16. 16. Sample output

×