Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should C# event handlers be exception safe?

Tags:

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 handlers should never throw?

like image 733
Marko Avatar asked Feb 24 '10 19:02

Marko


People also ask

When should C be used?

As a middle-level language, C combines the features of both high-level and low-level languages. It can be used for low-level programming, such as scripting for drivers and kernels and it also supports functions of high-level programming languages, such as scripting for software applications etc.

Should we use C?

The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.

Should C still be used?

C exists everywhere in the modern world. A lot of applications, including Microsoft Windows, run on C. Even Python, one of the most popular languages, was built on C. Modern applications add new features implemented using high-level languages, but a lot of their existing functionalities use C.

Should I learn C before C++?

There is no need to learn C before learning C++. They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own. Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.


1 Answers

Since invoking an event means that the caller has no knowledge about the callee:

  1. Invoking an event handler should be robust in the face of arbitrary exceptions. Everything up the call stack needs to clean up its own mess correctly, in case something completely unexpected occurs.

  2. Event handlers should really avoid throwing exceptions.

Things like null reference exceptions are really inexcusable in any code, so obviously we aren't concerned about that.

Things like file IO exceptions can always happen when writing or reading a file, so I would avoid ever doing IO within an event handler. If it makes sense to do IO in an event handler, then it also makes senst to handle the IO exceptions within the handler too. Don't propogate that back to the caller. Find some way to deal with it.

like image 84
Jeffrey L Whitledge Avatar answered Oct 07 '22 07:10

Jeffrey L Whitledge