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

后端 未结 4 811
广开言路
广开言路 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:27

    Not that I'm aware of. This is cumbersome, but it fully delineates you as the developer's intent:

    private bool inException = false;
    
    public void MyFunc1()
    {
      try
      {
        inException = false;
    
        // some code here that eventaully throws an exception
      }
      catch( Exception ex )
      {
         inException = true;
         Helper();
      }
    }
    
    public void MyFunc2()
    {
       inException = false;
       Helper();
    }
    
    private void Helper()
    {
        // how can I check if program execution is the  
        // result of a thrown exception here.
        if (inException)
        {
            // do things.
        }
    }
    

提交回复
热议问题