ooxi
ooxi

Reputation: 3349

Whats up with JDK exceptions which do not allow exception chaining?

There are a couple of exceptions I use regularly in the JDK which do not support exception chaining and have quite narrowed descriptions e.g.

Should such exceptions not be used outside of the JDK? If so, what alternative exception would you recommend? If the exceptions can be used outside of JDK (without code smell), how can I get exception chaining with those?

Upvotes: 1

Views: 78

Answers (1)

Dave G
Dave G

Reputation: 9777

There is a chaining capability but it is not given via a constructor argument.

You can look at Throwable.initCause(Throwable cause) which may serve your purpose, but I believe these exceptions were coded specifically for the purpose they indicate.

For ParseException, this is specific to the operation in question, while the NoSuchElementException is very specific to something like array or other sorts of linear Collections.

Upvotes: 4

Related Questions