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?
Are there techniques or third party tools that will generate warnings if code has
Share
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.