C# Pragma 抑制抛出错误时的中断
首先,我运行我的应用程序,并在任何错误(已处理或未处理)时抛出异常。
其次,我使用 TypeConverter 将用户输入字符串转换为实际对象。
第三个 TypeConverter
不提供 TryConvert
方法,因此我只能使用异常进行验证,在这里使用这段相当丑陋的代码:
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;
}
}
我发现 VS 中断相当分散注意力每次我输入 -1
或其他无效字符时都会执行。我可以使用类似于 this 的东西,但不是我要转换的所有类型也有一个 TryParse 方法。
我希望可能有某种方法可以在不更改异常设置的情况下禁用 try
中的代码部分的中断。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 try/catch 放入它自己的方法中,并在该方法上设置此属性:
调试器不会在该方法内停止(即使是断点)。当该方法完成时,异常已经被处理,因此它也不会在此时中断。
Put the try/catch in it's own method and set this attribute on the method:
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.
这不是直接答案,但您可以创建一个方法,在尝试使用 TypeConverter 之前对字符串值进行完整性检查,然后对其应用条件(“DEBUG”)属性 - 因此生产代码继续进行在调试时使用 TypeConverter(并捕获所有失败的情况),在使用 TypeConverter 之前会发现并避免常见错误。
通过应用条件,您可以避免在代码的发布版本中使用此代码 - 它只是用来捕获当前正在出现的常见错误。
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.
我不确定我是否完全理解您的问题,但如果您想在特定异常上禁用 VS 中断,您可以使用“异常”对话框 (ctrl-alt-e) 进行自定义。打开公共语言运行时异常树并深入到特定异常并将其关闭。 FormatException 位于 System.这样 VS 将在除 FormatException 之外的所有托管异常上中断。
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.