Parallel processing 如何在使用TPL时命中catch块上的断点
当我开始通过TPL理解时。我被困在这个密码里了。我有两个任务。Task1抛出ArgumentOutOfRangeException,Task2抛出NullReferenceException 考虑以下代码:Parallel processing 如何在使用TPL时命中catch块上的断点,parallel-processing,breakpoints,task-parallel-library,Parallel Processing,Breakpoints,Task Parallel Library,当我开始通过TPL理解时。我被困在这个密码里了。我有两个任务。Task1抛出ArgumentOutOfRangeException,Task2抛出NullReferenceException 考虑以下代码: static void Main(string[] args) { // create the cancellation token source and the token CancellationTokenSource tokenSour
static void Main(string[] args) {
// create the cancellation token source and the token
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
// create a task that waits on the cancellation token
Task task1 = new Task(() => {
// wait forever or until the token is cancelled
token.WaitHandle.WaitOne(-1);
// throw an exception to acknowledge the cancellation
throw new OperationCanceledException(token);
}, token);
// create a task that throws an exceptiono
Task task2 = new Task(() => {
throw new NullReferenceException();
});
// start the tasks
task1.Start(); task2.Start();
// cancel the token
tokenSource.Cancel();
// wait on the tasks and catch any exceptions
try {
Task.WaitAll(task1, task2);
} catch (AggregateException ex) {
// iterate through the inner exceptions using
// the handle method
ex.Handle((inner) => {
if (inner is OperationCanceledException) {
// ...handle task cancellation...
return true;
} else {
// this is an exception we don't know how
// to handle, so return false
return false;
}
});
}
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
我已为Task.WaitAlltask1、task2放置了try-catch块。理想情况下,它应该命中Catch块内ex.handler语句中的断点。据我所知,无论结果如何,它都应该击中挡块
如果我有task1.Result/task2.Result,同样的情况也会发生
我的问题是:在调试模式下,当我有意从任务中抛出断点时,为什么没有在catch块中命中断点,因为我想检查catch块下的语句。它只是在表示未经用户代码处理的NullReferenceException时加上黄色标记
Task task2 = new Task(() => {
throw new NullReferenceException();
});
我如何在挡块处到达断点
感谢回复:正如他们在评论中解释的,调试器在引发原始异常时暂停执行,因为线程不处理异常。如果继续执行EXCEION F5或“播放”按钮,程序应继续执行到您在继续中处理异常的位置。正如他们在注释中所解释的,调试器在引发原始异常的位置暂停执行,因为线程不处理异常。如果继续执行exceution F5或“播放”按钮,程序应继续执行到您在继续中处理异常的位置。当我运行此代码时,我确实得到了用户未处理的中断,因为该线程未处理该中断,但是,当我继续时,它会命中catch中的断点,并且捕获的aggregateeexception确实包含两个抛出的异常。当我运行此代码时,我确实得到了用户未处理的中断,因为它未被该线程处理,但是,当我继续时,它会命中catch中的断点,并且捕获的aggregateeexception确实包含两个抛出的异常。