Should C# event handlers be exception safe?

后端 未结 6 1862
梦如初夏
梦如初夏 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:42

    It's tempting to do this but ultimately, it depends on the situation.

    • If you want the program to crash rather than run the risk of running in a bad or weird state, then by all means throw an exception.
    • If you REALLY don't care (think hard about this one) and only need to note the exception, then wrapping in try/catch blocks make perfect sense.
    • If you want to give all of the event-handlers a chance to run before crashing (e.g, maybe some of the handlers release resources), then it requires a bit more effort...

提交回复
热议问题