How to implement one “catch'em all” exception handler with resume?

前端 未结 9 625
梦如初夏
梦如初夏 2020-11-29 07:07

I wonder how can I write a catch\'em all exception handler in the application level which will give the user the option to resume the application f

9条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 07:51

    Microsoft Enterprise Library Exception Handling Application Block has examples of how you can do this.

    Basically you surround the code that can throw exceptions with this:

    try
    {
      MyMethodThatMightThrow();
    }
    catch(Exception ex)
    {
       bool rethrow = ExceptionPolicy.HandleException(ex, "SomePolicy");
       if (rethrow) throw;
    }
    

    Then you can configure the Policy to show a dialog to the user and ask if she wants to continue.

    You still need to put try catch blocks around in your code at points where you believe you are at a consistent state.

提交回复
热议问题