所以基本上我想在C#中测试Tasks。
我的代码如下:
Task tm = new Task(Test);
tm.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted);
tm.Start();
void ExceptionHandler(Task task)
{
var ex = task.Exception;
ex.Handle((x) =>
{
if(x is PictureNotRecognized)
{
MessageBox.Show(String.Format("An exception occured" + Environment.NewLine + "Program aborted: {0}" + Environment.NewLine + "Step: {1}" + Environment.NewLine + "Error Message: {2}", (x as PictureNotRecognized).Aborted, (x as PictureNotRecognized).CurrentStep, (x as PictureNotRecognized).Message));
return true;
}
return false;
});
}
void Test()
{
throw new Exception();
}
因此,当我运行该应用程序时,它将引发异常并且Visual Studio停止。仅当我再次按F5时,它才继续并跳至
ExceptionHandler
。如何在Visual Studio中停用此功能?如果异常是由用户使用throw new XXXX
创建的,则它不会破坏程序
最佳答案
您可以在Debug-> Exceptions-> Command Language Runtime Exceptions-> System-> System.Exception-> Uncheck“User-unhandled”中禁用异常中断。
除中断仅在调试时使用,用户不会注意到异常中断!但是Visual Studio会通知您,您没有像Try Catch构造那样处理异常。有关使用Task处理异常的更多信息,请参见:
http://msdn.microsoft.com/en-us/library/dd997415(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/dd537614(v=vs.110).aspx
关于c# - 任务错误处理-Visual Studio停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22360905/