pascal said:
I got it to work...
Thanks to all, you provide the key ideas to get this to work.
You can cut and paste the following code into an IDE.
Hit the go button and the exception thrown depends on the array index
of the exception array.
Change the call to the method from x to y to z to see the effect.
I can now take these ideas and incorporate them into my program.
Bravo.
The approach I use is to declare one or two project-specific exceptions, one
checked and the other unchecked, e.g., FabulatorException and
FabulatorRuntimeException. Whenever the code catches a lower-level exception,
e.g., IOException, if I must rethrow it I rethrow the standard one with the
original one as the cause.
catch ( IOException ex )
{
String msg = "Some sensible log message with project-specific information. "
+ ex.getMessage();
logger.error( msg );
logger.debug( ex.getStackTrace() );
closeExternalResources();
throw new FabulatorException( msg, ex );
}
The times when I would rethrow a checked exception are few and far between in
any event.
catch ( IOException ex )
{
String msg = "Some sensible log message with project-specific information. "
+ ex.getMessage();
logger.error( msg );
logger.debug( ex.getStackTrace() );
closeExternalResources();
return ERROR;
}
The problems with the array approach are its verbosity and the reduction in
self-documentativity of the source. With only one custom Exception (or one
each of checked and unchecked), an array is completely superfluous anyhoo.
-- Lew