The correct way to throw and catch exceptions using async / wait

All please enter the following code:

Task<bool> generateStageAsyncTask = null;
generateStageAsyncTask = Task.Factory.StartNew<bool>(() =>
{
    return GenerateStage(ref workbook);
}, this.token,
   TaskCreationOptions.LongRunning,
   TaskScheduler.Default);

// Run core asynchroniously.
bool bGenerationSuccess = false;
try
{
    bGenerationSuccess = await generateStageAsyncTask;
}
catch (OperationCancelledException e)
{
    // Script cancellation.
    result.RunSucceeded = true;
    result.ResultInfo = "Script processing cancelled at the users request.";
    return result;
}

From the method GenerateStageI test and re-throw a OperationCancelledExceptionas follows

try
{
    ...
}
catch (Exception e)
{
    if (e.GetType() == typeof(OperationCanceledException))
        throw e;
    else // <- Here it is saying that the thrown exception is unhandled.
    {
        // Do other stuff...
    }
}

But the above line indicates that the repeated exception is unhandled. I wrap my own awaitin the first code snippet above with the corresponding one try/catch, why try/catchdoesn't this catch the repeated exception?

+3
source share
1 answer

Here is a support request for the same issue from Microsoft Connect . Disabling "Only my code" in

ToolsOptionsDebuggingGeneral

solves the problem.

+2
source

All Articles