我从Task.Run中捕获异常时遇到了问题.我更改了代码并解决了问题.我愿意弄清楚在这两种方式下处理Task.Run中的异常有什么区别:
在Outside函数中我无法捕获异常,但在Inside中我可以捕获它.
void Outside()
{
try
{
Task.Run(() =>
{
int z = 0;
int x = 1 / z;
});
}
catch (Exception exception)
{
MessageBox.Show("Outside : " + exception.Message);
}
}
void Inside()
{
Task.Run(() =>
{
try
{
int z = 0;
int x = 1 / z;
}
catch (Exception exception)
{
MessageBox.Show("Inside : "+exception.Message);
}
});
}
Run Code Online (Sandbox Code Playgroud)