How to detect if a program is executing under a thrown exception at runtime?

后端 未结 4 812
广开言路
广开言路 2020-12-18 07:54

Can I detect at runtime inside method Helper() that the program execution is the result of a thrown exception?

Note, my goal is to avoid extending method Helper()

4条回答
  •  失恋的感觉
    2020-12-18 08:41

    I think you are overthinking this. If you have an exception, pass an exception. If you don't, don't.

    Why don't you want to change the signature of the Helper() method?

    public void MyFunc1()
    {
      try
      {
        // some code here that eventually throws an exception
      }
      catch( Exception ex )
      {
         Helper(ex);
      }
    }
    
    private void Helper(Exception ex = null)
    {
        // result of a thrown exception here.
        if (ex!=null)
        {
            // do things.
        } else {
            // do other things
        }
    }
    

提交回复
热议问题