Search code examples
c#wpfmodularity

Exception throwing from C# modules


Our application is modulraized (Group of modules doing specific things). The modules have event handlers. These events could be fired from other modules or the application menu.

Situation:

Module A(which have UI) recieves an event "deleteitem". The event arguments should contain the item name to be deleted. But in this case it is null. Somewhere, somebody messed with something.

Question(s):

Should the Module throw? Rememeber, the module would be throwing inside an event handler and could crash the app as the module writer have no idea if the exception is handled.

The above scenario is a snaphot of a bigger question regarding throwing of excpetions from Modules which could result in application crashing. The argument against it is the application can continue working without the specific modules. Well then, who should ensure that - the module or the application?


Solution

  • If the item name is something that is expected to always be there and it being null is an exceptional circumstance, which should never happen, you should throw as now your application is in an unknown state that should never happen

    If this is something that the caller can recover from, they will write their own exception handling routines to handle it.