private void ProcessData(IProcessor processor)
{
Data data = new Data();
try
{
processor.Process(data);
}
catch( Exception e )
{
CustomCode(e);
}
}
Concrete IProcessor processor.Process method implementation:
public void Process(Data data)
{
try
{
Thing a = null;
a.MakeSomething();
}
catch( NullPointerException e )
{
DoSomething();
}
}
I would like to run CustomCode() method for all Exceptions appearing both in ProcessData() method or in its "try" block
(all code in call stack from ProcessData() till the end of stack which throws exceptions should be handled by multiple exception handlers - in this example both DoSomething() and CustomCode() should be run).
How can I achieve this? I know I can add "throw;" after "DoSomething();" but this will mean that every programmer in every implementation of the Processor should always remember to write "throw;"
in catch(...) so that CustomCode(e) can be run. Can it somehow be done better?
NullReferenceException(NPE is java, or do you have your own exception-class), but avoid it. Thus there´s no need to even care for the exception at all.