Unhandleable Exceptions
There is this wonderful error that sometimes happens when you’re running your .NET app on machines that are not your development machine:
Quote:
Application has generated an exception that could not be handled.
The first problem is identifying the offending code. For me, it has often been trying to run code without the appropriate SecurityPermission. With out worrying too much about how to acquire or test for SecurityPermissions, just know that if you don’t have the appropriate permissions, you will throw exceptions. The problem arises in the fact that (at least in .NET 1.1) even if you put the offending code in a ‘try’ block, it won’t help. What you need to do is to put it in a separate function, and call that function from the inside the ‘try’ block.
This behavior seems like a bug in .NET to me, or it may be a design ‘feature’, I don’t know. Whatever the case may be, this is how to deal with it.
Examples of common symptoms:
Create a new mutex without permission
Write or Read from registry without permission
Open, Create, Write, or Read from a file without permission
Example code in C#:
Wrong:
private void MyFunction()
{
try
{
Mutex m = new Mutex(true, "MyMutex");
}
catch (Exception)
{
//we'll never catch the exception, it behaves as a fatal exception
}
}
Correct:
private void MyFunction()
{
try
{
Mutex m;
GetMutex(out m);
}
catch (Exception)
{
//we catch the exception this time
}
}
private void GetMutex(out Mutex m)
{
m = new Mutex(true, "MyMutex");
}
Comments
Leave a Reply
You must be logged in to post a comment.