Upload
abid-khan
View
105
Download
0
Tags:
Embed Size (px)
Citation preview
Best PracticesException Handling
Abid Hossain Khan
Agenda
Advantages
Exception Hierarchy
Best Practices
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
Exception Hierarchy
Best Practice : 1
Never swallow exception in catch block
Best Practice : 2
Declare the specific checked exceptions that your method can throw
Best Practice : 3
Do not catch the Exception class rather catch specific sub classes
Best Practice : 4
• Never catch Throwable class
Best Practice : 5
Always correctly wrap the exceptions in custom exceptions so that stack trace is not lost
Best Practice : 6
Either log the exception or throw it but never do the both
Best Practice : 7
Never throw any exception from finally block
Best Practice : 8
Always catch only those exceptions that you can actually handle
Best Practice : 9
Don’t use printStackTrace() statement or similar methods
Best Practice : 10
Use finally blocks instead of catch blocks if you are not going to handle exception
Best Practice : 11
Throw early catch late
Best Practice : 12
Always clean up after handling the exception
Best Practice : 13
• Throw only relevant exception from a method
Best Practice : 14
Never use exceptions for flow control in your program
Best Practice : 15
Always include all information about an exception in single log message
Best Practice : 16
Use template methods for repeated try-catch
Best Practice : 17
Document all exceptions in your application in javadoc
Thank You