I ran an application in Visual Studio Code Analysis and in several methods I received the following message:
CA2200 Rethrow to preserve stack details 'fooBLO.FooMethod (DataProjectDTO, string)' rethrows a caught exception and specifies it explicitly as an argument. Use 'throw' without an argument instead, in order to preserve the stack location where the exception was initially raised. BLL FooBLO.cs 143
Example method:
public bool CadastrarSolicitacao(DadosProjeto oDadosProjeto, string tipoProcesso)
{
try
{
//Método que também utiliza Exception causando o "rethrows"
GeraSolicitacao solicitacao = PreencherGeracao(oDadosProjeto);
//Outras tratativas do método......
//..........
//Método que também utiliza Exception causando o "rethrows"
return _interfaceDAO.CadastrarSolicitacao(solicitacao);
}
catch (Exception e)
{
//Aqui utilizo o exception em "e" para gravar o log do erro (e.Message)
throw e;
}
}
I understood that in this method I get the alert because both the PreencherGeracao
method and the CadastrarSolicitacao
that are called by it already have a throw
and in the example method _interfaceDAO.CadastrarSolicitacao
it has the possibility to give another% with% being that this may have been sent from one of the methods called by it, and thus lose the trace.
Code Analysis itself directs you to use only throw
without the argument. Is this really the best practice?
The argument I am still using to write the system error log. I am aware that this often causes the write of more than one log error, because if the method throw
com% it will write a log and method example as well. Is that a bad practice? Should I write the log only on the last layer?