- Don't manage business logic with exceptions. Use conditional statements instead. If a control can be done with if-else statement clearly, don't use exceptions because it reduces readability and performance (e.g. null control, divide by zero control). .
- Exception names must be clear and meaningful, stating the causes of exception.
- Throw exceptions for error conditions while implementing a method. E.g. if you return -1, -2, -3 etc. values instead of FileNotFoundException, that method can not be understand.
- Catch specific exceptions instead of the top Exception class. This will bring additional performance, readability and more specific exception handling.
- Null control with conditionals is not an alternative to catching NullPointerException. If a method may return null, control it with if-else statement. If a return may throw NullPointerException, catch it.
- Try not to re-throw the exception because of the price. Bu if re-throwing had been a must, re-throw the same exception instead of creating a new exception. This will bring additional performance. You may add additional info in each layer to that exception.
- Define your own exception hierarchy by extending current Exception class (e.g. UserException, SystemException and their sub types) and use them. By doing this you can specialize your exceptions and define a reusable module/layer of exceptions.
- Use types of exceptions clearly. Fatal: System crash states. Error: Lack of requirement. Warn: Not an error but error probability. Info: Info for user. Debug: Info for developer.
- Don't absorb exceptions with no logging and operation. Ignoring exceptions will save that moment but will create a chaos for maintainability later.
- Don't log the same exception more than once. This will provide clearness of the exception location.
- Always clean up resources (opened files etc.) and perform this in "finally" blocks.
- Exception handling inside a loop is not recommended for most cases. Surround the loop with exception block instead.
Read more: CodeBuild
QR:
0 comments:
Post a Comment