I have some points to make:
- I very much disagree with "wrapping" exceptions. IMO you should NEVER replace exception A with exception B. When you build your part of an application, deciding that "the upper layer cannot ever handle the exception" you WILL severely limit usability of your code!
The example using the SQLexception is a good one: by percolating this to the upper layers they are FREE to handle them IF THEY WANT TO. Perhaps I want to close and reopen the connection and try again?
This decision should NEVER be made by any library code because it shifts too much knowledge of a process into the client code; this in term severely limits reusability.
In addition, by "reclassifying" exceptions you make it difficult to do proper debugging: the REAL reason for an error is hidden in a chained exception somewhere in the wrapped exception. If you have multiple layers of software wrapping exceptions makes it very difficult to find a real reason for a problem.
All this HAS the sad effect of having to specify throws clauses in much of the code used; this is imo best handled by just specifying a generic exception type in the throws clause. Which brings me to another point: I do not really see the need for checked exceptions - they are not helping to write proper code. So I find myself sadly siding with Microsoft here (ehh).
- A basic premise in exception handling should be that you only handle (catch) an exception if you KNOW what to do with it. For typical code this means that the code contains heaps of try .. finally blocks but hardly ever a try .. catch block! In your example catching the exception and just printing it to system.err is dumb indeed, but not knowing what to do with it and obfuscating (wrapping) the exception is imo as bad as that.
- And finally: I agree that exceptions should only be the "vehicle" for efficiently transporting error states thru the application.
They should not normally be used directly in reporting the problem to an end user except when you have made a special error exception class/class hierarchy which is able to report proper (i.e. user-understandable) and perhaps even i18n-ned messages.