Home

Bug: Only first thrown exception shown in click callback

edited December 2022

I came across an obscure bug in a script I had and was able to reproduce:

async Task Main()
{
    var keepRunning = Util.KeepRunning();
    var button = new Button("Run");

    button.Click += async (_, _) => await Run();

    button.Dump();

    async Task Run()
    {
        try
        {
            "Throwing inner".Dump();
            throw new Exception("Inner");
        }
        catch (Exception ex)
        {
            "Caught inner, throwing outer".Dump();
            throw new Exception("Outer", ex);
        }
        finally
        {
            keepRunning.Dispose();
        }
    }
}

After clicking on "Run", the script stops with the "Inner" exception shown. The outer exception is not shown at all. The code did fall into the block as you can see both messages.

I guess the problem was with the asynchronous event handler? When using the button constructor's onClick handler, the exception isn't shown at all (which is also a problem).

var button = new Button("Run", _ => Run());

Comments

Sign In or Register to comment.