views:

116

answers:

2

Hi,

I'm using VS2008 and have ReSharper too.

My question is how can I automate the creation of a try/catch block for which the catch is specifically populated with the possible exception from the try block? (i.e. not just put in an Exception ex)

Background - Trying to help follow the best practice I read of "Don't catch (Exception) more than once per thread. Good code throws exceptions as needed, and handles only the exceptions it knows how to handle."

thanks

+1  A: 

To do that I think you would need a tool that recursively walks your code (and the code it calls, and so forth), and figures out which exceptions could possibly be thrown.

Redgate has a tool that does this. However, I think you probably have a fair idea in your code of what possible exceptions it might throw, and the BCL has decent documentation that says what exceptions might possibly be thrown by any given method you call in the BCL, so it's probably not too hard at the local level to figure out what you need to handle and what you don't.

Robert Harvey
understood - but I'm lazy and want to save some time (it all adds up) plus I had this feature in my Java IDE that could do this in Java :)
Greg
@Greg: This was needed in Java because it was necessary to declare exceptions. .NET is a different world.
John Saunders
Ah, but Java forces you to declare the exceptions that might be thrown by a (??) method (sorry, it's been over 10 years). In c# there is no such requirement.
spender
@John - so when you do want to handle any exception (say you're doing some HTTP calls) do you normally have a catch for each possible exception? (i.e. to satisfy the best practice)
Greg
Personally, I don't do something just because it's "best practice". That's silly. You need to look at *why* it's considered best practice. Blindly catching all exceptions that the library throws is just as bad as blindly catching all exceptions - because you're still not thinking about what's going on. The reason you should write each exception individually is because **you need to think about what's going to happen when that exception is thrown**. If you're not doing that, then you're being an idiot, doubly-so if you're typing them out individually "because that's best practice".
Anon.
The Java concept of checked exceptions is, to my mind, a bit of a disaster that encourages developers to swallow exceptions with automatically generated try/catch pairs and has resulted in some deep hierarchies throwing astronomical amounts of exceptions and code bloat. That this is lacking in C# is by design. I don't think it would be such a bad thing to be able to declare exceptions for the IDE to tell you about, but the Java model ain't all that.
spender
@Greg: I would not catch all exceptions in that case. I catch only exceptions I can handle. Only exceptions for problems that I can correct. All other exceptions will be allowed to propagate until they reach a service or component boundary. At that point, I might catch `Exception`, log it, then rethrow.
John Saunders
+1  A: 

From what I've read, this was one of the major reasons why the C# team chose not to implement checked exceptions similar to Java - they wanted to discourage this kind of exception non-handling.

There is no means of intrinsically automating such an operation in Visual Studio, nor should you try; if you don't know how to handle a particular exception, then don't catch it just because the library might throw it.

In addition, there are all sorts of special-case system exceptions that might be thrown by the framework, like OutOfMemoryException, BadImageFormatException, AppDomainUnloadedException and so on, that aren't directly thrown by the particular method you're invoking but bubble through it from the .NET runtime. If you wanted to catch all possible exceptions then you'd have to catch these as well, but for the most part it would be a fruitless exercise since there's not much you can do when you get a StackOverflowException - and again, you generally shouldn't attempt to.

In conclusion, the best way to write exception handlers for library methods (and any other methods) is to read the documentation on said method, which will tell you what exceptions you can expect it to throw, and then catch only the ones you actually expect and/or know how to handle. Do not catch the generic System.Exception unless you want spectacular crashes and mysterious data corruption late into the production cycle.

Aaronaught
good advice - when I said 'all' I really just meant the ones listed against the method (e.g. getRequest) in the doco - my bad. So I really just meant a tool to auto generate these, but I know when I'm fighting a losing battle here :)
Greg
@Greg: Don't think of it as a battle, think of it as a journey. ;)
Aaronaught
the answer I created from spender's comment seems to have been deleted - not sure how to answer this question if one of the few comments that actually really answered the question (c.f. saying you should try to answer it) gets deleted? confused
Greg