Exception handling

From blog.UsToBe.com

Jump to: navigation, search
| 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.

Contents

Don't catch runtime exceptions

Never catch RuntimeException nor any subclass of RuntimeException.

Avoid 

try {
    ...
} catch (RuntimeException e) { //never catch RuntimeException
    ...
}

Avoid 

try {
    ...
} catch (NullPointerException e) { //never catch subclasses of RuntimeException
    ...
}

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 {
    ...
} catch (SQLException e) {
    //TODO do nothing
}

Avoid 

try {
    ...
} catch (SQLException e) {
    e.printStackTrace();
}

Tip use the following default catch clause

try {
    ...
} catch (SQLException e) {
    //TODO Auto-generated catch block
    throw new RuntimeException(e);
}

Don't catch Exception or Throwable

Never catch Exception or Throwable.

Avoid 

try {
    ...
} catch (Exception e) { //never catch Exception
    ...
}

Avoid 

try {
    ...
} catch (Throwable e) { //never catch Throwable
    ...
}

Always wrap exceptions

Avoid 

try {
    ...
} catch (SQLException e) {
    throw new RuntimeException(e.getMessage()); //loss of full stacktrace makes debugging difficult
}

Tip wrap the original exception

try {
    ...
} catch (SQLException e) {
    throw new RuntimeException(e); //original exception e is wrapped in RuntimeException, stacktrace is preserved
}

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
protected MemberVO performCreate() throws ApplicationException {
    //do nothing
}

Tip use UnsupportedOperationException for unimplemented methods

@Override
protected MemberVO performCreate() throws ApplicationException {
    throw new UnsupportedOperationException();
}

Personal tools