views:

214

answers:

2

In JBoss 4.2.2 (on JDK5), I'm noticing this behavior. Is there a configuration or other way to prevent it?

If I have code like this:

try {
  doSomething();
} catch (Exception e) {
  throw new EJBException(e);
}

The resulting stack trace (when caught and logged) will be:

EJBException ....
   at(.....
   at(.....

caused by: NullPointerException

There is no stack trace for the NullPointerException. Is there a way to prevent that swallowing of the stack trace and to actually have the full stack trace as part of the caused by?

A: 

Could be that EJBException isn't chainable.

I don't know if your example is representative of your real code, but I'd ask why you're catching an exception, wrapping it as an EJBException and rethrowing it? You don't add any meaningful new information by doing so. Your catch block doesn't even do the minimum of logging the original exception. So - why catch at all? Why not just declare the exception in your method signature and let clients handle it? The only good reason I can think of is EJBException is not a checked exception, so you're relieving clients of the obligation to catch it. But even that is dangerous. I would think you'd want to be sure that the exception didn't propagate across layer boundaries. What good will it do a UI to see a raw, uncaught exception?

duffymo
If you throw checked exceptions, transactions don't get rolled back. EJBException accepts the exception in its constructor, although it is certainly possible that it uses its own legacy chaining code rather than what was introduced in JDK1.4, or otherwise destroys the stack trace, although I would expect that to be part of the container. I agree the exception handling here is bad, but I have a lot of legacy code with it, so I'm trying to at least salvage getting good stack traces.
Yishai
+2  A: 

It's an implementation decision, they don't set the original cause as the cause of the ejbException. You can access to that looking the method getCausedByException(). You have a JIRA[1] about that (take a look at this because there's a painful workaround), and you can take a look to the JBoss AS implementation[2].

[1]: https://jira.jboss.org/jira/browse/EJBTHREE-337

[2]: http://viewvc.jboss.org/cgi-bin/viewvc.cgi/jbossas/trunk/jboss-j2ee/src/main/javax/ejb/EJBException.java?revision=39121&view=markup&sortby=rev&pathrev=39121

diega
Thanks. Do you know if this is fixed at all in the 4.2.x branch? I see the issue is fixed, but I can't figure out where they put the fix and where they didn't.
Yishai
I think they put it as fixed because is the way they want this to behave :-/
diega