Reputation: 12896
Let's say I have the following method in C#:
XslCompiledTransform myObject;
public void foo() {
try {
myObject.Transform(input, output);
} catch (???) {
}
}
Is there a menu option or functionality in Visual Studio 2012 which automatically generates all the catch
blocks for the exceptions which might occur in XslCompiledTransform
? Like "Generate catch blocks"?
Upvotes: 4
Views: 1661
Reputation: 5203
No, there is no smart way to automatically generate all catch blocks of all possible exception. But Visual Studio shows related exception list to function call as shown below.
Upvotes: 1
Reputation: 48985
I personally don't think it's generally a good practice. IMO you should only catch exception you consider possible to be thrown.
This means in your example:
ArgumentNullException
in your example, but check that input
/output
are not null before calling your methodIOException
instead of DirectoryNotFoundException
/FileNotFoundException
unless you have a different exception handling for both casesRemember catching specific exceptions is only interesting is you have a specific exception handling associated. If it's just "log then re-throw", then don't catch specific exceptions.
EDIT: I assume you are looking for a tool that does the job. I'm not aware of such a tool (you might want to check on visualstudiogallery). What's interesting though, is that Exception Hunter, a tool from RedGate that was doing this job, has been discontinued for interesting reasons. Have a look:
With the release of .NET 4.0 and WPF, the number of exceptions that the CLR can throw was greatly increased, to the point of being overwhelming. The exclusions list can no longer cover all the unlikely exceptions that the CLR may throw. This means that, although Exception Hunter will provide accurate results, these results will include a long list of potential exceptions, most of which are nothing to worry about. In essence, the tool has become a lot less usable and makes your job harder than it should be. This goes against our ingeniously simple ethos, so we have decided to stop selling new licenses for the product.
Upvotes: 3
Reputation: 62246
No it's not bad practise tll it fits your needs. It's hard to say if it good for you or not,as it depends on your app structure and expected behaviour.
Usually try to catch them on highest logical level possible, where you are flexible enough on your app to make decisione wither throw
it or handle it in some way.
Upvotes: 2