Tuesday, July 03, 2007

Exception handling in java


About Exception-Handling Antipatterns
Tim McCune show us in an "old" post (2006) several ways of do the wrong thing with exceptions. Without borrow the post, here are some of my favorite antipatterns:

Log and Throw
(Duplicate entries in the error logs)
catch (NoSuchMethodException e) {
e.printStackTrace();
throw new MyServiceException("Blah", e);
}


Throwing Exception
(Too ambiguous)
public void foo() throws Exception


Catch and Ignore
(Destroys the exception information)
catch (NoSuchMethodException e) {
return null;
}


Destructive Wrapping
(Destroys the original exception)
catch (NoSuchMethodException e) {
throw new MyServiceException("Blah: " +
e.getMessage());
}

No comments: