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 st开发者_开发百科ring 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.
Put the try/catch in it's own method and set this attribute on the method:
[System.Diagnostics.DebuggerNonUserCode]
The debugger will not stop inside that method (even for breakpoints). And when the method is finished, the exception has already been handled so it doesn't break at that point either.
Under the Debug -> Exceptions menu you can turn of breaking for any particular exception type.
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.
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.
精彩评论