Re: Some same exceptions used in a given file
Merciadri Luca wrote:
Patricia Shanahan writes:
I can see two options for reducing the amount of duplicate code:
1. Put the try-catch for IOException at a high enough level in the call
hierarchy, and wide enough scope, that it will catch all instances of
IOException for which you want the same handling.
2. Extract the handling for IOException into a method that can be called
from multiple catch blocks.
And what is the difference between try and catch at a high enough
level and the same, but with a throw in the method that might generate
an exception?
The difference is where they fit in your strategy. The catch at the "high
enough" level will not throw the lower-level exception (e.g., 'IOException'),
but the lower-level routine will. That's the difference.
"High enough" means at a layer where lower layers are subject to, for example,
'IOException'. At that point, where all lower layers log-and-rethrow the
exception, the higher-level method will catch the exception and convert it
into a legitimate interaction.
You *did* read my earlier post about exception strategy, interaction by
contract and conversion thereto, did you not?
Take the advice you've been given and think about it. You're not going to get
all your insights spoon-fed to you via Usenet. Programming is art that
requires the practitioner *himself* to think.
So here's how that "higher-level" routine might look:
public Status orchestrate( InputData data )
{
Status rval;
try
{
somethingThatMightThrow( data );
somethingElseThatRisksIt( data );
yetSomethingElseThatCanThrow( data );
rval = Status.SUCCESS;
}
catch ( IOException exc )
{
logger.error( LOWLEVELEXC, exc ); // suitable String
rval = Status.SCREWUP;
}
return rval;
}
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg