Handling a class that doesn't throw exception in c# -
I have some UI code that looks like this:
try {SomeClass classInstance = New Class (some ID); } Grip (exception exception) {// Content was not created, to show a message, stop processing return; }
It seems that the attempt was put on hold because the Constructor will be bombed for some classes if some IID is not received, and the data can not be found in DB.
Recently running this code through FXCop, it warns against using common exceptions, but everything class throws a new exception with the message that it starts Has failed.
I guess the problem is that the class constructor should have its own custom exception, which I can handle in my UI, but I wonder what else to do with the exception to handle the exception Can I meet the FXCop requirements? The rule of FxCop exists because the above hold (exception)
block low- Level catches all possible exceptions, including exceptions, such as stackoverflowexception
that you might not be holding in a useful way.
The right approach is definitely to throw a more specific type: Aid, or an existing one. The NET Framework exception type that matches your situation closely. (When there is a doubt, I usually go to Invalid Operation Exposure
.)
Alternatively, you can see the exact exception type while holding it, not to the FxCop alert But it should resolve the underlying problem:
hold (exception exception) {if (exception.GetType () == typeof (exception)) {// Content was not created, Show a message, stop the processing return; } Else {// Some other exception types which were not thrown from our code - // reps throw high-level exception handlers; }}
Comments
Post a Comment