Question

In Visual Studio 2010, is it possible to prevent first chance exceptions from writing to the Output window? We have a scenario where there are lots of casts that are failing by design, and the output window is really slowing down the application when debugging. Writing out A first chance exception of type 'System.InvalidCastException' occurred... over and over just takes a long time. The console in Visual Studio is not fast :<)

I'm aware of the option to not break on first chance exceptions, but that doesn't seem to affect the output windows. Neither does putting this before the area that is slowing us down:

Debug.Listeners.Clear()

Closing the Output window also does not help. It runs very fast in release mode though.

Any help is appreciated!

Was it helpful?

Solution

I can answer this for Visual Studio 2013:

In VS 2013 you can go to the DEBUG menu - Options and Settings... - Debugging - Output Window. Under "General Output Settings" you find "Exception Messages". Turn it off.

OTHER TIPS

Per @Iridium's comment, I wound up changing to a Try pattern and return a bool as a success flag instead of throwing an InvalidCastException. Looks a lot like this:

if (!property.CanAssignValue(valueToSet))
{
    Debug.Write(string.Format("The given value {0} could not be assigned to property {1}.", value, property.Name));
    return false;
}
property.SetValue(instance, valueToSet, null);
return true;

The "CanAssignValue" became three quick extensions:

public static bool CanAssignValue(this PropertyInfo p, object value)
{
    return value == null ? p.IsNullable() : p.PropertyType.IsInstanceOfType(value);
}

public static bool IsNullable(this PropertyInfo p)
{
    return p.PropertyType.IsNullable();
}

public static bool IsNullable(this Type t)
{
    return !t.IsValueType || Nullable.GetUnderlyingType(t) != null;
}

Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top