java exceptions best practices
DESCRIPTION
Java Exceptions Best PracticesTRANSCRIPT
![Page 1: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/1.jpg)
Exceptions in Java
![Page 2: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/2.jpg)
An exception is an event, which occurs during the execution of a program, that
disrupts the normal flow of the program.
![Page 3: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/3.jpg)
Exception Hierarchy
Unchecked
CheckedError
![Page 4: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/4.jpg)
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.
![Page 5: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/5.jpg)
Exception is a part of API
![Page 6: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/6.jpg)
Basic example
![Page 7: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/7.jpg)
What’s Wrong Here ?
![Page 8: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/8.jpg)
Basic example in Java 7
![Page 9: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/9.jpg)
What else is in Java 7
![Page 10: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/10.jpg)
Custom Exception
![Page 11: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/11.jpg)
Exceptions in JUnit
![Page 12: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/12.jpg)
Exceptions in JUnit
![Page 13: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/13.jpg)
Best Practices
• Never swallow the exception in catch block.
• Always throw specific exception.
![Page 14: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/14.jpg)
Best Practices
• Catch specific sub-classes.
• Include cause, otherwise stack trace will be lost.
![Page 15: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/15.jpg)
Best Practices
• Either log the exception or throw it.
• Never return or throw from finally block.
![Page 16: Java Exceptions Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b736504a7959404e8b45be/html5/thumbnails/16.jpg)
Thank you!