VS15 can add conditions to exceptions!

Last week at (or rather during) //Build conference a new Visual Studio was unveiled. You can download the preview right now – I’ll wait…

Getting a new Visual Studio feels like Christmas morning (if I was celebrating Christmas). There’s always cool features to explore and new things to play with, an improved shade of purple of the infinity icon – good stuff!

I was browsing the VS15 release notes (not to be confused with the previous VS2015) when I saw a cool feature I always wanted. In fact I was asked for that specific feature during one of the many OzCode’s demos. That feature is the ability to add an condition to when (or rather where) to break on an exception.

.NET exceptions are great, they help me understand when my code misbehave and I get to use quite a lot. The problem is catching those tricky bastards. Usually when I try to squash a bug that cause an exception I want to break when that exception is thrown. To do so I would usually open the Exception settings dialog and tell Visual Studio to break when a specific exception is thrown.

So, if I have a console application that uses two external libraries both with bugs, but only one I care about:

static void Main(string[] args)
{
    var mineAllMine = new MyClassLibrary.MyClass();
    var buggyCode = new OtherClassLibrary.ExternalClass();

    try
    {
        buggyCode.SomeMethod();
    }
    catch (Exception)
    {
        Console.WriteLine("Buggy code exception");
    }

    try
    {
        mineAllMine.AmazingMethod();
    }
    catch (Exception)
    {
        Console.WriteLine("Performed according to spec!");
    }
}

And since I cannot fix the external class (and its buggy code) we want to only break on exceptions thrown from our own superior (and amazing) method.

Doing so with VS15 is simple simple open Visual Studio’s Exception settings and choose, or better yet search for the exception you want mark it and then press the small pencil icon to add an condition.

image

Right now we can only choose on which module to break (or which module not to break).

And it works like a charm.

IMHO Debugging just got better and hopefully will continue to improve.

 

Until then – happy coding…

Labels: , ,