1

EXAMPLE - A() -> B() -> C()

C() contains a try-catch, and simply logs the exception and carries on with execution, because the throw; was commented out.

Therefore the try/catch in A() won't trigger.

public void A(){
   try{
      B();
   }
   catch(Exception e){
      //something really important
   }
}


public void B(){
   try{
      throw new Exception("test");
   }
   catch(Exception e){
      Log.Error($"Error - {e.InnerException}");
      //throw;
   }

   //further code
}

Is there any mechanism that forces A() catch to capture ANY exception raised in the call-stack following it?

The reason I ask is I need to ensure any issues with a Payment are reported but there must be 40+ try catches in the masses of code that B(); contains!

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
aspirant_sensei
  • 1,568
  • 1
  • 16
  • 36
  • 1
    No exception is thrown at that point in the stack, so there's nothing to catch. The way to "force" a `catch` is to throw an exception. – David Jun 21 '17 at 11:14
  • 1
    Why `throw` is commented? Btw, you can use c# 7.0 [exception filters](https://stackoverflow.com/a/4268291/1997232) to log something. – Sinatr Jun 21 '17 at 11:15
  • @David the exception is thrown in the try of method B, and is caught within method B, so I understand as it wasn't re-thrown A is not aware but I was wondering if any mechanism exists to do so. – aspirant_sensei Jun 21 '17 at 11:24
  • @Sinatr just for demonstration purposes :) In reality the code does some minor cleaning and continues execution, but I really want to ensure ANY 'cleaning' that has occurred notifies the calling function too (again, simplistic demonstration) – aspirant_sensei Jun 21 '17 at 11:24
  • Inside of A you could create a static method that you call from inside of the catch block in B, passing the exception accross to notify it of any exceptions caught. -- edit just noticed A was not a class – uk2k05 Jun 21 '17 at 11:33

2 Answers2

3

No, you can't catch an already handled exception unless that exception is rethrown:

try
{
   B();
}
catch(Exception e)
{
    // handle the exception

    throw; // then re-throw it.
}

If it is for logging purposes, you might be looking for the AppDomain.FirstChanceException event, which gives you the ability to 'catch' exceptions even when they are handled.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks, it's for logging purposes which is essentially me not wanting to try and find every single Exception in the potential code execution paths. I am glad you pointed out that event because it looks exactly like what I was after. – aspirant_sensei Jun 21 '17 at 11:26
1

No

There is not. You will need to trust B() to do it's job and accurately report on the results.

There is no protection from B() being this either:

public bool B()
{
   var failed = ImportantOperation();

   if(failed) 
      // oops
      return true;
   else
      return true;
}

If you don't trust code, write it yourself or get a code provider you can trust.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142