Should C# event handlers be exception safe?

后端 未结 6 1864
梦如初夏
梦如初夏 2021-02-05 07:14

Assuming that one event has multiple handlers, if any of event handlers throw an exception then the remaining handlers are not executed.

Does this mean that event handle

6条回答
  •  野的像风
    2021-02-05 07:53

    In an ASP.NET application, I'd just let the event handlers throw exceptions. Those exceptions will then allow the custom error page functionality of ASP.NET to work.

    In a desktop application, I always add a try/catch/finally block around the guts of any event handler. This way, any exceptions are always logged and displayed to the end-user, but never escape the event handler, so they don't crash the application.

提交回复
热议问题