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.
Exceptions in Java
An exception is an event, which occurs
during the execution of a program, that
disrupts the normal flow of the program.
Exception Hierarchy
Unchecked
CheckedError
Three Kinds of Exceptions
• Checked - subject to the Catch or Specify Requirement.
• Application can recover from it.
• Er...
Exception is a part of API
Basic example
What’s Wrong Here ?
Basic example in Java 7
What else is in Java 7
Custom Exception
Exceptions in JUnit
Exceptions in JUnit
Best Practices
• Never swallow the exception in catch block.
• Always throw specific exception.
Best Practices
• Catch specific sub-classes.
• Include cause, otherwise stack trace will be lost.
Best Practices
• Either log the exception or throw it.
• Never return or throw from finally block.
Thank you!
Upcoming SlideShare
Loading in …5
×

Java Exceptions Best Practices

457 views

Published on

Java Exceptions Best Practices

  • Be the first to comment

  • Be the first to like this

Java Exceptions Best Practices

  1. 1. Exceptions in Java
  2. 2. An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program.
  3. 3. Exception Hierarchy Unchecked CheckedError
  4. 4. Three Kinds of Exceptions • Checked - subject to the Catch or Specify Requirement. • Application can recover from it. • Errors - external to the app, app can’t recover from. • Hardware or system malfunction. • Unchecked – not a subject to the Catch or Specify. • Application cannot recover from it. • Usually app bugs.
  5. 5. Exception is a part of API
  6. 6. Basic example
  7. 7. What’s Wrong Here ?
  8. 8. Basic example in Java 7
  9. 9. What else is in Java 7
  10. 10. Custom Exception
  11. 11. Exceptions in JUnit
  12. 12. Exceptions in JUnit
  13. 13. Best Practices • Never swallow the exception in catch block. • Always throw specific exception.
  14. 14. Best Practices • Catch specific sub-classes. • Include cause, otherwise stack trace will be lost.
  15. 15. Best Practices • Either log the exception or throw it. • Never return or throw from finally block.
  16. 16. Thank you!

×