Reputation: 67
Conditional exception handling?
We are using C#. I am trying to create a system within our software that tracks and restores the data a user was working on should a crash occur.
We have existing restoration code that reloads a completed work set and I am supposed to call these functions. We want to avoid duplicated code.
However my data is, by definition, incomplete and I'm an hitting issue where when a field is null, no further fields are loaded.
The people who wrote these functions want the exceptions thrown so they know when they have a problem with data.
I know I have a problem with the data and am just trying to get as much back as possible.
Is there a way to do some sort of
a) conditional try/catch so that you can control it to the point of being able to set when exceptions are handled and when they are thrown?
or
b) some sort of "on error resume next" capability, but with the additional caveat that it can be turned off. This is being ported from VB6 and "resume next" was how it was handled there, but they don't want to use that this time - this time they want to know about the errors. If, however, (if it exists, and) it could be turned on and off, it would work for both of us.
Any ideas?
Thanks all!
Upvotes: 1
Views: 495
Reputation: 67
Added a bunch of try/catch blocks using the "when" clause. Code became
try { function1() } catch when (!NormalMode) {}
They get their exceptions while I get my catches.
Upvotes: 0
Reputation: 578
If you know the specific errors that you want to recover from and log. You can catch those specifically.
in the catch block just log, and do what ever logic you need to recover. Then you can have a more generic exception catch to throw unexpected errors.
Upvotes: 1