Search code examples
.netexceptionchecked-exceptions

How would I know if I haven't handled some unchecked exceptions that my .NET code could throw?


In .NET, method signatures don't tell me if I have missed handling some exceptions that could be thrown by my code. Is there some tool that can warn me, if say I am using a HashTable remove but haven't handled the ArgumentNullException? I don't want to be surprised at run time.

And does this mean that you need to know your code very well, otherwise unchecked exceptions can be harder to work with?


Solution

  • Actually, handling unexpected exceptions by terminating the program is the best way to deal with this situation, since, in general, the program state is undefined when something unexpected happens. If you log all exceptions, and have a decent set of acceptance tests, you can eliminate problems which come from unexpected exceptions due to program flow.

    Defensive programming by checking method inputs, unit tests of classes and understanding your framework will make most exceptions expected.