Catching Exception in RMI
Hello,
I have this problem. I have this method in my RMI IF
public Object performSomething() throws RemoteException
{
try
{
doSomething();
}
catch(Exception e)
{
RemoteException eEx = new RemoteException(e.getMessage(), e.getCause());
eEx.setStackTrace(e.getStackTrace());
throw eEx;
}
}
but when i logged the Exception that i caught (as well as the stack trace), i got null for the exception message and null also for each message in the stackTraceElement. Does anyone has an idea why this has happened.
And btw, the exception that i excepted was an sql exception.
Thanks a lot =)
[696 byte] By [
gigsz1a] at [2007-11-26 23:47:57]

# 1
That's because you're constructing your own, and destroying all the information in the original exception. Just cast it to RemoteException, don't construct a new one, or better still just catch the RemoteException (and the SQLException directly:
catch (RemoteException exc)
{
// ...
}
catch (SQLException exc)
{
// ...
}
It is rarely correct to catch Exception except in toy code or at the bottom of Runnable.run() methods.
Indeed, it is also rarely best to log and rethrow. This is a well-known antipattern. The catcher that has to finally catch the exception and do something about it should log it too.
ejpa at 2007-7-11 15:23:07 >
