Exception handling
From blog.UsToBe.com
| You are here: Home > Best Practices > Exception handling
Exception handling is an extremely important aspect of developing applications that are stable, easy to maintain and debug.
In fact a good exception handling approach is essential in achieving an optimal development productivity. Bad exception handling practices on the other hand can be responsible for many hours wasted on debugging and problem solving.
Don't catch runtime exceptions
Never catch RuntimeException nor any subclass of RuntimeException.
| Avoid |
try { |
| Avoid |
try { |
TODO add another example with parseInt() throwing NumberFormatException
Don't add runtime exceptions to the throws clause of a method
Never add RuntimeException nor any subclass of RuntimeException to the throws clause of a method.
| Avoid |
public void perform(args) throws RuntimeException { //never add RuntimeException to throws clause |
| Avoid |
public void perform(args) throws NullPointerException { //never add subclass of RuntimeException to throws clause |
Don't add Exception to the throws clause of a method
Never add Exception to the throws clause of a method.
| Avoid |
public void perform(args) throws Exception { //never add Exception to throws clause |
Don't write catch clauses that do nothing
| Avoid |
try { |
| Avoid |
try { |
| Tip use the following default catch clause |
try { |
Don't catch Exception or Throwable
Never catch Exception or Throwable.
| Avoid |
try { |
| Avoid |
try { |
Always wrap exceptions
| Avoid |
try { |
| Tip wrap the original exception |
try { |
Don't create new Exception classes
Don't create new classes that extend an existing Exception class. The framework should provide the necessary exception handling classes.
| Avoid |
public class MySQLException extends SQLException { |
Use java.lang.UnsupportedOperationException when implementation is missing
| Avoid |
@Override |
| Tip use UnsupportedOperationException for unimplemented methods |
@Override |

