views:

220

answers:

3

First off I run my applications with exceptions thrown on any error (handled or not).

Second I am using a TypeConverter to convert from a user input string to the actual object.

Third TypeConverter offers no TryConvert method so I'm stuck using exceptions for validation, using this rather ugly bit of code here:

try
{
    this._newValue = null;
#pragma Magic_SuppressBreakErrorThrown  System.Exception
    this._newValue = this.Converter.ConvertFromString(this._textBox.Text);
#pragma Magic_ResumeBreakErrorThrown  System.Exception
    this.HideInvalidNotification();
}
catch (Exception exception)
{
    if (exception.InnerException is FormatException)
    {
        this.ShowInvalidNotification(this._textBox.Text);
    }
    else
    {
        throw;
    }
}

I'm finding it rather distracting to have VS break execution every-time I type the - of -1, or some other invalid character. I could use something similar to this but not all the types I'm converting to have a TryParse method either.

I'm hoping there may be some way to disable breaking for the section of code within the try without changing my exception settings.

+1  A: 

Under the Debug -> Exceptions menu you can turn of breaking for any particular exception type.

Adam Ruth
+1  A: 

I am not sure I follow your question entirely, but if you want to disable VS break on specific exceptions you can customize this using the Exceptions dialog (ctrl-alt-e). Open the Common Language Runtime Exceptions tree and drill down to the specific exception and turn that off. FormatException is located under System. That way VS will break on all managed exceptions except FormatException.

Brian Rasmussen
That is indeed the effect I'm looking for, unfortunately it throws a System.Exception not FormatException. I was hoping to still be able to break on all, except if it occurs within an explicitly defined region of code (see edit).
Courtney de Lautour
A: 

Not a direct answer, but you could create a method that does a sanity check on the string values, before you attempt to use the TypeConverter, and then apply the Conditional("DEBUG") attribute to it - so the production code goes ahead and uses the TypeConverter (and catches all failing cases) whilst while debugging, your common errors are picked up and avoided before hitting the TypeConverter.

By applying the conditional, you avoid this code being used at all in the release version of your code - it's just there to catch the common errors which are currently creeping in.

Damien_The_Unbeliever