coding best practices_exception handling
TRANSCRIPT
![Page 1: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/1.jpg)
Best PracticesException Handling
Abid Hossain Khan
![Page 2: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/2.jpg)
Agenda
Advantages
Exception Hierarchy
Best Practices
![Page 3: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/3.jpg)
Advantages
Represent a meaningful message to user
Log unhandled exception once and only once
Correlate exceptions with unique request id (session) in log file for high precision debugging
Implement powerful but extensible exception handling strategy for each tiers
![Page 4: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/4.jpg)
Exception Hierarchy
![Page 5: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/5.jpg)
Best Practice : 1
Never swallow exception in catch block
![Page 6: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/6.jpg)
Best Practice : 2
Declare the specific checked exceptions that your method can throw
![Page 7: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/7.jpg)
Best Practice : 3
Do not catch the Exception class rather catch specific sub classes
![Page 8: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/8.jpg)
Best Practice : 4
• Never catch Throwable class
![Page 9: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/9.jpg)
Best Practice : 5
Always correctly wrap the exceptions in custom exceptions so that stack trace is not lost
![Page 10: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/10.jpg)
Best Practice : 6
Either log the exception or throw it but never do the both
![Page 11: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/11.jpg)
Best Practice : 7
Never throw any exception from finally block
![Page 12: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/12.jpg)
Best Practice : 8
Always catch only those exceptions that you can actually handle
![Page 13: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/13.jpg)
Best Practice : 9
Don’t use printStackTrace() statement or similar methods
![Page 14: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/14.jpg)
Best Practice : 10
Use finally blocks instead of catch blocks if you are not going to handle exception
![Page 15: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/15.jpg)
Best Practice : 11
Throw early catch late
![Page 16: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/16.jpg)
Best Practice : 12
Always clean up after handling the exception
![Page 17: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/17.jpg)
Best Practice : 13
• Throw only relevant exception from a method
![Page 18: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/18.jpg)
Best Practice : 14
Never use exceptions for flow control in your program
![Page 19: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/19.jpg)
Best Practice : 15
Always include all information about an exception in single log message
![Page 20: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/20.jpg)
Best Practice : 16
Use template methods for repeated try-catch
![Page 21: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/21.jpg)
Best Practice : 17
Document all exceptions in your application in javadoc
![Page 22: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/22.jpg)
![Page 23: Coding best practices_exception handling](https://reader036.vdocuments.us/reader036/viewer/2022081519/55c44008bb61ebd16b8b481d/html5/thumbnails/23.jpg)
Thank You