C# Pragma to suppress break on thrown error

Posted by Courtney de Lautour on Stack Overflow See other posts from Stack Overflow or by Courtney de Lautour
Published on 2010-03-25T06:53:29Z Indexed on 2010/03/25 11:23 UTC
Read the original article Hit count: 609

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about pragma