Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 804
Abhijeet Kashnia Avatar asked Feb 05 '10 06:02

Abhijeet Kashnia


People also ask

What happens if you dont handle unchecked exceptions?

An unchecked exception (also known as an runtime exception) in Java is something that has gone wrong with the program and is unrecoverable. Just because this is not a compile time exception, meaning you do not need to handle it, that does not mean you don't need to be concerned about it.

What happens when unchecked exception is thrown?

If a program throws an unchecked exception, it reflects some error inside the program logic. Java does not verify unchecked exceptions at compile-time. Furthermore, we don't have to declare unchecked exceptions in a method with the throws keyword.

Why runtime exceptions are not checked?

Because the Java programming language does not require methods to catch or to specify unchecked exceptions ( RuntimeException , Error , and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException .


4 Answers

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.

like image 81
codekaizen Avatar answered Oct 29 '22 06:10

codekaizen


For windows applications:

AppDomain currentDomain = default(AppDomain);
currentDomain = AppDomain.CurrentDomain;
// Handler for unhandled exceptions.
currentDomain.UnhandledException += UnhandledExceptionHandler;
// Handler for exceptions in threads behind forms.
Application.ThreadException += ThreadExceptionHandler;

public static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
}

public static void ThreadExceptionHandler(object sender, Threading.ThreadExceptionEventArgs e)
{
}
like image 45
Aseem Gautam Avatar answered Oct 29 '22 06:10

Aseem Gautam


There is no language support for what you are attempting to accomplish. We wrote a customs addin to VS to make sure that all the entry points for our API had try catches for logging. However I never saw the value in languages that forced you to cover all possible declared exception cases since you still have to write some code to do something meaningful with the error. Most people just look at what the complier is complaining about write a handler and hide what could be a useful error in some useless code. In some cases its better to fail and know there is a problem.

like image 43
rerun Avatar answered Oct 29 '22 05:10

rerun


You should add topmost level exception handler for your application and write unit, functional and integration test for your app, which will test all possible use cases. This will help you to eliminate almost all unchecked exceptions.

Also try not to catch exceptions, but eliminate the reason. (i.e. Not catch ArgumentNullException but do not passing null

like image 35
uthark Avatar answered Oct 29 '22 05:10

uthark