dotnet / dotnet/command-line-api

How do we implement exception handling around Command.Invoke?

Open
#796 40 comments 16 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

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`?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.