Annotated Exceptions
Probably the main reason people don’t do this in Java is because the Exception class doesn’t provide any default mechanism for storing extra data. To that end I wrote some new Throwable base classes for my project at work that allow arbitrary annotations to be attached to an exception before it’s thrown. (I call them AntException, AntRuntimeException, etc….). Not only can I annotate the exception with contextual data, I can mimic the Java 1.4 feature of chained exceptions in Java 1.3 by annotating an exception with another exception.
The problem is that most people are thinking about the non error path when they write the code and not about how they will debug a post-mortem problem in the code from a log file. A very simple case of how doing this little extra work has helped is this story. I work on a very large team spread literally across the world. On any given day there are people running any of a half dozen builds from the past week. As exceptions show up in the log, folks capture the log fragments and write bug reports (or Software Problem Reports as we call them). One very simple thing we did to make this information more valuable was to annotate every exception with a build version number. (Sure the tester should know what version of the code they are running and put that in the bug report, but this eliminates any doubt). So now when I look at an exception dump in a log file I not only know where the problem occurred, but what build it was that had the problem.
Comments