Search code examples
c#exceptionpragma

C# Pragma to suppress break on thrown error


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.


Solution

  • 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.