exception handling wrapper

  • Thread starter Thread starter z. f.
  • Start date Start date
Z

z. f.

Hi,

is there a way to create some kind of class that you can set to have event that will fire when exception is throws?

instead of having a try .. catch block
whenever the class is in scope, is exception is thrown a functio (event) is called, and the class can act to handle the error is a single method (closing resources etc) instead in every inportant method?

TIA, z.
 
There's no way to do this in the current framework but Whidbey has an exception thrown event that you can subscribe to. However, the best method for dealing with exceptions is to wrap the thread/method call with a try-catch in the appropriate places, otherwise all your exceptions will be unhandled and there are consquences to that (e.g. your app may terminate).

The above is how you deal with errors - in a catch block. The issue of closing resources is related but separate. Resources should be closed in a finally block, not the catch block. It can also be encapsulated in the Dispose method of the object itself, which can be invoked from the finally block. I consider finally blocks as a mechanism for cleaning up code paths (i.e. sequential logic) and Dispose methods as mechanisms for cleaning up resource allocations of an object. You can invoke Dispose methods from finally blocks, so one does not preclude the other. However, objects that implement Dispose can be wrapped in a using statement (which is an implicit try-finally construct) inside of larger try-finally constructs.

I understand your desire to minimize the amount of boilerplate code you have to generate, but you need enough code for it to work properly, and no less (and no more, either).
Hi,

is there a way to create some kind of class that you can set to have event that will fire when exception is throws?

instead of having a try .. catch block
whenever the class is in scope, is exception is thrown a functio (event) is called, and the class can act to handle the error is a single method (closing resources etc) instead in every inportant method?

TIA, z.
 
Back
Top