ARTICLE AD BOX
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); }1,0361 gold badge11 silver badges22 bronze badges
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);110k21 gold badges215 silver badges279 bronze badges
Explore related questions
See similar questions with these tags.
