views:

54

answers:

3

I'm debugging a service that's experiencing some problems on start-up. To aid me in this, I'm wrapping pretty much everything in a try/catch block, and writing any errors to a file. I don't want to put them in every method, I just want to put them in the highest level methods so that they catch exceptions from other methods.

Something is getting through, though, as the service does stop under some conditions. Is there a way to determine where the gaps in my try/catch coverage are, other than by sight?

+1  A: 

try to try/catch main function and all thread functions. This should fill the gaps, unless third party code spawns threads

Andrey
I'm pretty sure I have that, which is what's perplexing me.
Mike Pateras
are you sure that no threads are spawned? do you want to do it for debugging or you plan to use this logging in production?
Andrey
Threads are spawned, I just made sure that I was catching exceptions inside of them. I'm doing this for debugging purposes.
Mike Pateras
I suppose I could have missed one, though. That's what I was hoping to determine via this post.
Mike Pateras
+4  A: 

You can catch AppDomain level exceptions like this:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

void CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
{

    //Object exception = e.ExceptionObject;
    //Boolean terminating = e.IsTerminating;
}

You can check out this link for more examples.

Matt Dearing
especially if app is written in native c++ ;)
Andrey
I tried doing this, but I don't think it caught an exception. I subscribed to the event in the first line of my service's OnStart method, and in the handler method, I made the call to my logging method, like this: Utility.LogError(e.ExceptionObject as Exception); However, my service appears to have shut down, but I have no log. This should catch exceptions on other threads, too, right?
Mike Pateras
+1  A: 

You didn't specify the environment (windows-service could mean managed or unmanaged). I'm going to guess you're working in native code, probably C or C++. If that's the case you need to be certain you're using structured exception handling from windows and not the C++ try/catch mechanism. Depending on your compiler vendor, the C++ implementation can't catch all exceptions.

In managed code, depending on framework version there are unhandleable exceptions. Excluding those, you also can fail to catch an exception if you don't prepare a constrained region and carefully follow the CER rules. Also as someone else mentioned, you can watch unhandled exceptions in your AppDomain. (You may not be in a single app domain but this is uncommon and you'd probably know if you weren't).

One more thing to remember in either case is that each thread has it's own stack and set of exceptions. If you're trying to try/catch all exceptions, you need to catch every thread that is working on your behalf. In managed code, the finalizer thread (garbage collector thread that executes "destructors" or finalizer code) is one place that is often missed.

However, before I jumped to either of those, I'd look for a process exit or a non-exception exit clause.

Peter Oehlert
Sorry, I'm working in managed code. C# to be specific.
Mike Pateras