Visual Studio—>Debug—>Exceptions

April 4, 2011

ASP.NET, C#, Visual Studio 2010

Yesterday I was told about an option in Visual Studio I had not previously been aware of, and boy, am I excited about it!

Debug—>Exceptions (Ctrl+D,E)

DebugExceptions

Break when an exception is: Common Language Runtime Exception: Thrown is un-checked by default.

DebugExeptionsWindow

Why do I care? How can this help me?

How many times have you seen code like this, where not only a good practice (like TryParse to avoid throwing an expensive exception) has not been implemented, but it’s also been wrapped in a try/catch without actually being handled.

PreExistingBadSwallow

Until yesterday’s new tip, I have found it close to impossible to reactively determine where code like this exists, since it doesn’t thrown an exception or break during debug when the exception is swallowed like that.

When you Check “Common Lanuguage Runtime: Thrown” prior to debugging, those swallowed CLR Exceptions will break when they’re thrown, no matter what the programmer’s “Handling” involved.

CLRFormatException

In this example, the code above could be replaced by something like this, which would prevent an exception from being thrown in the first place:

CLRExceptionFound&Fixed

I am so excited about this I want to shout it from the rooftops…. right after I optimize a few inherited and/or legacy projects Smile Woo hoo!

Thank you to Ryan Brown for showing me this!!!

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: