The divide by zero error that gets thrown on purpose in the below code doesn't get handled by the UnhandledExceptionTrapper function because it is within a System.CommandLine SetAction function.

Does anyone know how unhandled exceptions that happen within SetAction methods can be included in this?

using System.CommandLine; AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper; var testCommand = new Command("test"); testCommand.SetAction(args => { int x = 0; int y = 15 / x; }); RootCommand rootCommand = new RootCommand { testCommand }; await rootCommand.Parse(args).InvokeAsync(); static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine("Inside UnhandledExceptionTrapper"); Console.WriteLine(e.ExceptionObject.ToString()); System.Environment.Exit(1); }

Paul's user avatar

By default System.CommandLine will handle with its own handler.

You can override this by passing a InvocationConfiguration to Invoke or InvokeAsync, checking that EnableDefaultExceptionHandler is false.

var parse = myRootCommand.Parse(Args); var invokeCfg = new InvocationConfiguration { EnableDefaultExceptionHandler = false, ProcessTerminationTimeout = null }; var result = await parse.InvokeAsync(invokeCfg, canTok);

Richard's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.