views:

189

answers:

3

In a recent project I'm using a lot of databinding and xml-serialization. I'm using C#/VS2008 and have downloaded symbol information for the .NET framework to help me when debugging.

The app I'm working on has a global "catch all" exception handler to present a more presentable messages to users if there happens to be any uncaught exceptions being thrown. My problem is when I turn on Exceptions->Thrown to be able to debug exceptions before they are caught by the "catch all". It seems to me that the framework throws a lot of exceptions that are not immediately caught (for example in ReflectPropertyDescriptor) so that the exception I'm actually trying to debug gets lost in the noise. Is there any way to get rid of exceptions caused by the framework but keep the ones from my own code?

Update: after more research and actually trying to get rid of the exceptions that get thrown by the framework (many which turn out to be known issues in the framework, example: http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor) I finally found a solution that works for me, which is turning on "Just my code" in Tools >> Options >> Debugging >> General >> Enable Just My Code in VS2008.

+2  A: 

One way to filter exceptions thrown by the framework is to derive your own exception classes from Exception. You can then use multiple catch blocks:

try
{
//your code here
}
catch (MyAppException myEx)
{
//Do something with your custom exception
}
catch (Exception ex)
{
//Do something (or nothing) with an exception thrown by the Framework
}
Dave Swersky
+5  A: 

You can fine tune which types of exceptions are caught in which way through the Exceptions dialog in VS2008, but If the frame work is throwing "a lot of exceptions" you might want to look into that as well. Just because an exception is handled doesn't mean that it's safe, or that it's not robbing your performance.

David Hay
Usually, if the framework is throwing a lot of exceptions, you're doing something wrong.
Martinho Fernandes
Yes, which is why I posted what I did.
David Hay
@David: I wasn't making a point against your answer, I was trying to reinforce it (I +1ed). Sorry if I couldn't express myself better :)
Martinho Fernandes
I agree, this happens on startup when a form is deserializing settings into a bunch of databound controls. I don't really know this code very well and I don't see the exceptions without the debug symbols so it hasn't actually been made a priority.
Hans Løken
+1  A: 

It might be worth your while to get rid of catch-all exceptions as it is not exactly good programming technique. For an example, if you are working with Input Output such as file reading/writing, trap the IOException instead of the more generic Exception, another example is XmlException for any XML manipulation. Using a catch-all generic Exception may yield a performance hit as the specific exception has to "bubble up" up to the generic Exception Handler.

Hope this helps, Best regards, Tom.

tommieb75
That's a good idea! Temporarily disabling it will at least let me debug the exception when it happens. Thank you.
Hans Løken