0

I have a complex wpf-application, where I am using some third-party components. Occantionally my application crashes in specific windows, and I can't figure out why.

Is there a way to catch an exception for an entire window?

I tried the following, but it did not work.

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var window1 = new Window1();
        window1.Owner = this;
        try
        {
            window1.Show();
        }
        catch { }


    }
}

And the childwindow:

 public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        int a = 0;
        int b = 1 / a; //This will throw an exception
    }
}

Any suggestions?

  • could you _please_ be more precise about "it did not work"? – Franz Gleichmann Feb 06 '21 at 12:01
  • 1
    For the window, or the application? Why not just run it in the debugger with "Break on Thrown" enabled and then VS will stop as soon as the faulty code is encountered – Caius Jard Feb 06 '21 at 12:54
  • 1
    @Franz Gleichmann; By "Did not work" I mean that the application crashes. I would assume that window1 would close, but mainwindow would remain open. But this does not happen, so obviously the exception is not caught. – Ole Haahr Andersen Feb 06 '21 at 13:16
  • @Caius Jard: This is actually a general question, if there is a way to catch an exception for a form, by putting it into a try-catch.. – Ole Haahr Andersen Feb 06 '21 at 13:20
  • Please consider whether the techniques discussed here: https://stackoverflow.com/questions/1472498/wpf-global-exception-handler could adequately serve your needs (your Q might be a duplicate of them - I'm not willing to VTC as a dupe of it though..) – Caius Jard Feb 06 '21 at 13:35
  • @@Caius Jard Thank you for the link. It lead me to a a solution – Ole Haahr Andersen Feb 08 '21 at 05:30

0 Answers0