Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
| Proprietà | valore |
|---|---|
| ID regola | CA2200 |
| Title | Eseguire il rethrow per mantenere i dettagli dello stack |
| Categoria | Utilizzo |
| La correzione causa un'interruzione o meno | Non rompente |
| Abilitato per impostazione predefinita in .NET 10 | A titolo di avvertimento |
| Linguaggi applicabili | C# e Visual Basic |
Causa
Un'eccezione viene rilanciata e specificata in modo esplicito nell'istruzione throw.
Descrizione regola
Una volta generata un'eccezione, parte delle informazioni che contiene è la traccia dello stack. L'analisi dello stack è un elenco della gerarchia di chiamate al metodo che inizia con il metodo che genera l'eccezione e termina con il metodo che intercetta l'eccezione. Se un'eccezione viene rilanciata specificandola nell'istruzione throw, il traceback dello stack è riavviato nel metodo corrente e l'elenco delle chiamate di metodo tra il metodo originale che ha generato l'eccezione e il metodo corrente viene perso. Per mantenere le informazioni dello stack trace originali con l'eccezione, utilizzare l'istruzione throw senza specificare l'eccezione.
Se stai rigenerando l'eccezione da un punto diverso dal blocco del gestore (catch), usa ExceptionDispatchInfo.Capture(Exception) per acquisire l'eccezione nel gestore e ExceptionDispatchInfo.Throw() quando vuoi rigenerarla.
Per altre informazioni, vedere Catturare e rilanciare correttamente le eccezioni.
Come correggere le violazioni
Per correggere una violazione di questa regola, generare nuovamente l'eccezione senza specificare l'eccezione in modo esplicito.
Quando eliminare gli avvisi
Non sopprimere un avviso da questa regola.
Esempio
Nell'esempio seguente viene illustrato un metodo, CatchAndRethrowExplicitly, che viola la regola e un metodo, CatchAndRethrowImplicitly, che soddisfa la regola.
class TestsRethrow
{
static void Main2200()
{
TestsRethrow testRethrow = new();
testRethrow.CatchException();
}
void CatchException()
{
try
{
CatchAndRethrowExplicitly();
}
catch (ArithmeticException e)
{
Console.WriteLine($"Explicitly specified:{Environment.NewLine}{e.StackTrace}");
}
try
{
CatchAndRethrowImplicitly();
}
catch (ArithmeticException e)
{
Console.WriteLine($"{Environment.NewLine}Implicitly specified:{Environment.NewLine}{e.StackTrace}");
}
}
void CatchAndRethrowExplicitly()
{
try
{
ThrowException();
}
catch (ArithmeticException e)
{
// Violates the rule.
throw e;
}
}
void CatchAndRethrowImplicitly()
{
try
{
ThrowException();
}
catch (ArithmeticException)
{
// Satisfies the rule.
throw;
}
}
void ThrowException()
{
throw new ArithmeticException("illegal expression");
}
}
Imports System
Namespace ca2200
Class TestsRethrow
Shared Sub Main2200()
Dim testRethrow As New TestsRethrow()
testRethrow.CatchException()
End Sub
Sub CatchException()
Try
CatchAndRethrowExplicitly()
Catch e As ArithmeticException
Console.WriteLine("Explicitly specified:{0}{1}",
Environment.NewLine, e.StackTrace)
End Try
Try
CatchAndRethrowImplicitly()
Catch e As ArithmeticException
Console.WriteLine("{0}Implicitly specified:{0}{1}",
Environment.NewLine, e.StackTrace)
End Try
End Sub
Sub CatchAndRethrowExplicitly()
Try
ThrowException()
Catch e As ArithmeticException
' Violates the rule.
Throw e
End Try
End Sub
Sub CatchAndRethrowImplicitly()
Try
ThrowException()
Catch e As ArithmeticException
' Satisfies the rule.
Throw
End Try
End Sub
Sub ThrowException()
Throw New ArithmeticException("illegal expression")
End Sub
End Class
End Namespace