1
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?

4
  • If you want customerCode to always happen regardless of an exception then there is a design flaw. Why not remove the try catch from the process() and have processdata() handle it. If the exception is handled its handled, you need to choose which should handle what. Commented Dec 22, 2017 at 9:04
  • 1
    You shouldn´t catch a 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. Commented Dec 22, 2017 at 9:07
  • Seabizkit I have another IProcessor implementations. In one of them I commit transaction to database. If exception occurs after commiting this transaction I would like to rollback this transaction. Each IProcessor implementation should have another exception handling code, depending on what Process(Data data) is doing. Commented Dec 22, 2017 at 9:42
  • You can use delegates.If I understand properly ,you want CustomCode and DoSomething to be called whenever an exception occurs. Commented Dec 22, 2017 at 9:52

2 Answers 2

1

I agree with @Seabizkit comment, you might not want to have try-catch in Process implementation or at least throw; the exception in the end of catch block of Process for hiding it is a bad practice.

Otherwise, the solution might be to have a finally block in ProcessData() where you check if the data was processed (e.g. a special flag was set) and if not, that means that something went wrong and CustomCode() has to be executed, but then you don't know what kind of exception that was. Something like:

private void ProcessData()
{    
    Data data = new Data();

    try
    {
        Processor processor = new Processor();
        processor.Process(data);
    }
    catch( Exception e )
    {
        CustomCode(e);
    }

    finally {
        if (!data.IsProcessed) CustomCode();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I agree that solution with finally block is correct :)
1
public void Process(Data data)
{
  try
  {
    Thing a = null;
    a.MakeSomething();
  }
  catch( NullPointerException e )
  {   
    CustomCode();
    DoSomething();
  }
  catch(Exception ex)
  {
   CustomCode();
 }

}

1 Comment

I want to implement handling exceptions on ProcessData method level, because I have many implementations of Processor classes. I will modify my code that is in question to not confuse you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.