最佳答案
I had a problem with catching the exception from Task.Run
which was resolved by changing the code as follows. I'd like to know the difference between handling exceptions in these two ways :
In the Outside
method I can't catch the exception, but in the Inside
method I can.
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);
}
});
}