Reputation: 3349
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.
NoSuchElementException
, "Thrown by the nextElement
method of an Enumeration
to indicate that there are no more elements in the enumeration" (updated to "Thrown by various accessor methods to indicate that the element being requested does not exist" in JDK 8)ParseException
"Signals that an error has been reached unexpectedly while parsing."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
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