我有一个引发错误的后台任务(对象引用未设置为对象的实例)。我希望调试器在发生异常时停止,但事实并非如此。我启用了所有“公共语言运行时异常”。我很确定这与使用async / await有关。
以下代码复制了该行为。该程序的输出符合预期: 你好! 错误:xxx
class Program
{
public static void Main(string[] args)
{
new Test().TestMethod();
Console.WriteLine("Hello!");
Console.ReadLine();
}
}
public class Test
{
public async void TestMethod()
{
try
{
await Task.Run(() =>
{
throw new Exception("xxx"); // Debugger should stop the execution here
});
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}