Topic 07: Exception Handling


Published on


Published in: Education
1 Like
  • Be the first to comment

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

No notes for slide

Topic 07: Exception Handling

  1. 1. Topic 07 : Exception Handling DDOOCP
  2. 2. Learning Outcomes › Understand the importance of Defensive Programming; › Use the Exception Class to improve the robustness of a Java project; › Catch system generated Exceptions using the try – catch structure; › Display system-generated Exception messages; › Display programmer generated Exception messages; › Use the finally block; › Design a testing strategy and associated test data.
  3. 3. Problems What will happen during compilation? What will happen during runtime?
  4. 4. Problems
  5. 5. Problems
  6. 6. Defensive Programming › An exception is an exceptional event or error that occur during program execution and interrupt the flow of the program. › Defensive programming is the tactic of writing code to reduce future, unanticipated problems i.e. exception. › It can be done by reducing code complexity, validating all user input and data. › This display of red lines of error messages is called a stack trace. The stack trace lists the classes and methods that the exception passed through before the program was aborted.
  7. 7. Types of Exception › The exceptions are broadly classified into two categories, namely, 1. checked exceptions and 2. unchecked exceptions.
  8. 8. Checked Exceptions › Exceptions that must be handled explicitly by the code are called checked exceptions. › e.g. SQLException, IOException › In java, java.lang.Exception is a checked exception and all its subclasses will also be checked. › For checked exceptions, you either have to put a try/catch block around the code or add a "throws" clause to the method, to indicate that the method might throw this type of exception.
  9. 9. Unchecked Exceptions › Exceptions that do not need to be handled explicitly are called an unchecked exception. › e.g. ArithmeticException, NumberFormatException. › In java java.lang.RuntimeException is an unchecked exception and so are all its subclasses.
  10. 10. Built-in Exceptions
  11. 11. Checked Exceptions 1. FileNotFoundException 2. IOException 3. ClassNotFoundException
  12. 12. Unchecked Exceptions 1. ArithmeticException 2. IndexOutOfBoundsException 3. ArrayIndexOutOfBoundsException 4. NullPointerException 5. NumberFormatException 6. StringIndexOutOfBoundsException
  13. 13. Exception Handling Techniques › The try and catch blocks › Multiple catch blocks › The finally block › Nested try blocks › The throw clause › The throws clause
  14. 14. The try and catch blocks
  15. 15. The try and catch blocks
  16. 16. Displaying error messages
  17. 17. Multiple catch Blocks
  18. 18. Multiple catch Blocks › Write a program that handles ArrayIndexOutOfBoundsException, ArithmeticException, NumberFormatException and Exception. Demo MultipleCatch
  19. 19. The finally Block › The code in the finally block is executed regardless of whether an exception is thrown or not. › You can have only one finally block for an exception-handler.
  20. 20. The finally Block
  21. 21. The nested try block › try block within try block
  22. 22. Exception Example
  23. 23. Same example using function
  24. 24. The throws clause
  25. 25. The throws clause › The throws clause is used when we know that a method may cause exceptions, but the method does not handle those exceptions. › In such a case, a user has to throw those exceptions to the caller of the method by using the throws clause. › A throws clause is used when a method is declared.
  26. 26. The throw clause › Till now, we have learned about catching exceptions thrown by JRE. › In this section, we will learn to throw exceptions explicitly i.e. custom exception. › We can throw an object of any exception type in Java by using the new operator with throw clause.
  27. 27. The throw clause example
  28. 28. Same example using anonymous object
  29. 29. Same example using function
  30. 30. Same example using OOP concept
  31. 31. Using throws and throw clauses
  32. 32. Custom Exception class
  33. 33. References › › exception-class-usage