Exception Handling Rules
exception, rule, tip ·Goal
To get an advice for exception handling.
Rules
-
Clean Up resources using Finally Block or Try-With-Resources
Don’t forget to clean used resources up.
-
Prefer Specific Exceptions
Don’t handle exceptions in ambiguous way.
-
Document the Exception You Specify
Add
@throwsin you javadoc. -
Throw Exceptions With Descriptive Messages
Specify the reason the exception happened.
-
Catch the Most Specific Exception First
Don’t catch its super class exception before.
-
Don’t Catch Throwable
Of course you can catch some throwables too, but don’t.
-
Don’t Ignore Exceptions
That exception might not that important or not like to happen for now, but you never know.
-
Don’t Log and Throw
If you were gonna re-throw it forward it to handle it later, why would you log it?
-
Wrap the Exception Without Consuming It
Stack your exceptions, even when making new one.