Tuesday, January 01, 2008

javax.mail.MessagingException and compatibility issues?

javax.mail.MessagingException has a constructor with parameters String and Exception. However if you instantiate do
MessagingException me = new MessgingException("abc", e);
and invoke me.getCause() and if you expect to get exception e which you imbedded in constructor you are wrong.
Because MessagingException constructor use the embedded exception to set setNextException(). It does not call super("abc", e);
Hence if you want to get root cause, use MessagingException.getNextException() instead of getCause()
Why this wiered api design? Probably MessagingException API might have been designed before JDK 1.4 where we didn't have feature for embedding one exception into another.
Now we are not in a position to change the API to ensure backward compatibility! Otherwise anybody who is using getNextException will fail.
Probably MessagingException.getCause() could override to return nextException!

No comments: