2

Are there techniques or third party tools that will generate warnings if code has a potential of throwing an exception which is not wrapped in a try/catch block?

4
  • 4
    Why Should there? Usually it is very good to not catch an exception.
    – Uwe Keim
    Dec 6, 2011 at 19:09
  • 7
    Are you in the habit of wrapping all code in try{}catch{} blocks? From your question it appears that you are not using exceptions correctly.
    – Oded
    Dec 6, 2011 at 19:09
  • I have a custom Exception class that I throw in some cases. These exceptions should be caught because I don't want the application to come to a grinding halt. They are not as critical as out of memory exceptions or the like.
    – MarkP
    Dec 6, 2011 at 19:13
  • 2
    But can they actually be handled? I think you're missing the point; exceptions should only be caught when there is a reasonable course of action to take in response. Otherwise your app should come to a "grinding halt" (i.e., fail fast). If you can't trust your coworkers to write correct code then you have bigger problems I am afraid. Dec 6, 2011 at 19:18

3 Answers 3

3

I would suggest that you might be better off thinking about why code is generating exceptions, as opposed to simply and without further consideration demand that all code be wrapped in some try/catch/finally or another. (Incidentally, if this were possible, it would be trivial to make it go away - developers could just wrap the main entry point in a try/catch, and no more warnings).

Code Contracts is a tool that won't do what you're asking for here, exactly, but it will help you reason about your code's run time behavior at compile time via compiler warnings. This will tell you things like when a client call deferences a return value and the service method might return null. This is getting to the root of exception-causing behaviors rather than just demanding that you handle them. In this case, you have a bad programming assumption -- you don't want to catch this exception, you want to eliminate it by preventing it from happening in the first place.

I would save exception handling for places where you're dealing with externalities that are beyond your control (e.g. the user unplugs the network cable while your app is downloading a file). It sounds to me as if you're looking for Java-style checked exceptions, which C# does not have. But, if you look at the method XML comments for external libraries, you can see what exceptions they might generate, and handle those specific cases. If you're only really doing this for externalities (and using Code Contracts to manage internal assumptions), the amount of additional time spent won't be much.

0

There is a question with roughly the same problem. Which is how to detect if a code could throw an error. I'm not sure if this question would count as a dupe, at it seems you want to do it pre-compiled, getting warnings

However, the answer to the accepted question over there:

Following up to my previous answer, I've managed to create a basic exception finder. It utilises a reflection-based ILReader class, available here on Haibo Luo's MSDN blog. (Just add a reference to the project.)

[...]

To summarise, this algorithm recursively enumerates (depth-first) any methods called within the specified one, by reading the CIL instructions (as well as keeping track of methods already visited). It maintains a single list of collections that can be thrown using a HashSet object, which is returned at the end. It additionally maintains an array of local variables and a stack, in order to keep track of exceptions that aren't thrown immediately after they are created.

If you don't mind doing a code analysis post-compilation, and based on IL-code, you could try using the snippet Noldorin posted in that question.

0

Along with Erik's answer (upvoted his):

Exceptions are there to provide a way of notifying the system that something abnormal has happened. They are not there to provide a convenient way of letting a method communicate with the calling code.

If your methods have pass/fail situations then they should be returning a Boolean flag. If they are a little more complicated, such as needing to return status codes, then you should use a struct or class as the result.

To sum up: if you find yourself using a lot of custom exceptions, then you're doing it wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.