The public API should never throw 'Exception' because it forces the
API user to catch RuntimeExceptions, and he may not want to do that. I
think a good API only throws one kind of Exception, for example
SlingException (maybe one or two subclasses, but the JCR API has way
too many in my view).

The implementation could internally use 'throws Exception' to avoid
'throws a, b, c, d', and convert the Exception at the outermost level
(in all methods that are part of the API). This should be done in any
case for logging (even catch Throwable). RuntimeExceptions and Errors
can be re-thrown as is (use a converter utility to avoid copy &
paste).

Still I wouldn't internally throw Exception, because you want to add
context information to the exception. Example:

copyFile(String fileA, String fileB) throws SlingException {
 try {
   ...
 } catch(IOException e) {
   throw new SlingException("Can not copy " + fileA + " to " + fileB, e);
 }

Important is to include the original exception (this is often forgotten).

Thomas

Reply via email to