Neo*_*isk 8 .net c# vb.net exception task-parallel-library
我在MSDN上研究了这篇文章,以及有关此主题的SO的一些问题/答案,但无法理解为什么下面的代码不起作用(在示例控制台应用程序中).
根据MSDN,预计会抛出AggregateException,它将包含一个带hello消息的内部异常.相反,此hello异常未处理.它发生在调试器内部.
如果您按继续或独立运行,它将按预期工作.有没有办法避免在VS中一直按下继续?毕竟,无论是内部的Try...Catch块被认为是处理在单线程编程模型.否则,调试可能是一场噩梦.
VB.NET
Sub Main()
Try
Task.Factory.StartNew(AddressOf TaskThatThrowsException).Wait()
Catch ex As AggregateException
Console.WriteLine(ex.ToString) 'does not get here until you hit Continue
End Try
End Sub
Private Sub TaskThatThrowsException()
Throw New Exception("hello") 'exception was unhandled
End Sub
Run Code Online (Sandbox Code Playgroud)
C#
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
try {
Task.Factory.StartNew(TaskThatThrowsException).Wait();
}
catch (AggregateException ex) {
Console.WriteLine(ex.ToString()); //never gets here
}
}
static void TaskThatThrowsException() {
throw new Exception("hello"); //exception was unhandled
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有什么明显的遗失吗?
设置“仅启用我的代码”对此有影响。在“工具”->“选项”下,“调试”->“常规”-> 启用“仅我的代码”。如果您打开它,如果您的代码不处理异常,它将认为该异常未处理。尝试关闭此选项。
请参阅: http ://msdn.microsoft.com/en-us/library/dd997415.aspx