Don’t Forget Your Root (Cause)
August 30, 2006 – 00:32 | javaWe all know that this is bad:
JAVA:
-
try {
-
// ...
-
} catch (DaoException e) {
-
// do nothing.
-
}
But some of us (namely the dude who wrote the code that drove me crazy trying to troubleshoot today) apparently didn't know this was almost equally bad:
JAVA:
-
try {
-
// 200 lines of code with many dao calls.
-
} catch (DaoException e) {
-
throw new AppException(e.getMessage());
-
}
What's wrong with that is there is no way to tell at exactly which point DaoException was thrown, since the original stack traces are lost. So, my fellow Java programmers, please, please always do:
JAVA:
-
try {
-
// 200 lines of code with many dao calls.
-
} catch (DaoException e) {
-
throw new AppException(e.getMessage(), e); // <--- always pass along the cause!
-
}
The only time should you be using the message-only constructor is when AppException is the root cause itself.

2 Responses to “Don’t Forget Your Root (Cause)”
What about doing?
...
catch (DaoException e) {
throw new AppException(e);
}
By lowell on Sep 6, 2006
Sure, that is fine. Although I would always add some message that is sensible in the wrapping context.
By Jing Xue on Sep 8, 2006