2
Why it is not possible to capture exceptions triggered by non-recurring asynchronous methods Task
?
public async void calcularPrecos()
{
var tabelaPreco = await getTabelaPreco();
/* Blá, blá, blá... */
}
public void iniciarCalculos()
{
try
{
calcularPrecos();
/* Blá, blá, blá... */
}
catch (WebException e)
{
/* A exceção é disparada mas não é capturada neste ponto. =( */
}
}
The method getTabelaPreco()
requires internet connection, otherwise it triggers an exception of type WebException
, but I cannot capture such an exception in try-catch
inside iniciarCalculos()
.
Maybe creating a [mcve] will make it easier to identify the problem.
– Maniero
Dear @bigown, this is a real example, I thought the consequent lines would not add in solving the problem.
– vinibrsl
If it were real it would give to compile it. I continue with my suggestion to do in the way indicated there. And it is not to put all the code, only the minimum to check the condition of the error, at the moment can not be sure what this code does and why the exception is not captured, it can be up because it was not launched.
– Maniero
Friend, your method
iniciarCalculos
it is not asynchronous, it will not work right (probably also that this is the problem). Use reserved wordsasync
andawait
in this method just as you did in thecalcularPrecos
.– Gabriel Katakura
Related link https://github.com/dotnet/roslyn/issues/13897
– vinibrsl