Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does WPF swallow an exception thrown in an event handler for Window.Activated?

Simple WPF app with a plain, empty Window in which I hook up an event handler to the Window's Activated event:

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        Activated += OnWindowActivated;
    }

    private void OnWindowActivated(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}

When an exception is thrown in the handler and unhandled anywhere else, I expected the app to die, but it doesn't. WPF seems to be swallowing the exception somewhere and the Window pops up and keeps on running just fine.

Why?

like image 825
mjl5007 Avatar asked Dec 18 '25 15:12

mjl5007


1 Answers

This could be an issue related to running 32-bit applications on a 64-bit operating system. If that is the case then the blog post by Paul Betts should give you a good idea what is happening.

In short it looks like for 32-bit processes the .NET framework eats your exceptions due to issues with the propagation of the exception across user-mode / kernel mode boundaries. For 64-bit processes this doesn't happen, so the simplest way to test if this is indeed your problem is to rebuild your test app with for the 'Any CPU' platform and run it again. When I did that it crashed as one would expect.

like image 84
Petrik Avatar answered Dec 21 '25 05:12

Petrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!