dotnet / dotnet/command-line-api
How do we implement exception handling around Command.Invoke?
- Vorherrschende Sprache
- C#
- Sterne
- 3.7k
- Forks
- 428
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
I have created a simple .NET Core console application with this line to execute the `Command`
```
try
{
rootCommand.Invoke(args)
}
catch
{
Console.Error.Write("Something bad happened");
}
```
But when the command handler associated to the command throws an exception, the catch block isn't executed. Instead, the Invoke method returns a non-zero integer.
So, what's the correct method to handle exceptions around `Invoke`/`InvokeAsync`?
Beitragsleitfaden
Rechercherichtung
Beginne mit Command.Invoke und InvokeAsync und verfolge dann, wie Ausnahmen aus dem zugehörigen Command-Handler zu einem Rückgabewert ungleich null werden, statt den catch-Block des Aufrufers zu erreichen. Vergleiche dieses Verhalten mit dem try/catch-Beispiel und bestimme den erwarteten Vertrag zur Ausnahmebehandlung für beide Einstiegspunkte.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- csharp
- Bereich
- cli
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100