Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3999304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:38:25+00:00 2026-05-20T07:38:25+00:00

A WPF window dialog is shown using the ShowDialog method in the Window class

  • 0

A WPF window dialog is shown using the ShowDialog method in the Window class like when a button is pressed on the main window, like this.

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var window = new Window1();
                window.ShowDialog();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show("I am not shown.");
            }
        }

The window has a Loaded event subscribed in the xaml like this:

<Window x:Class="Stackoverflow.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Loaded="Window_Loaded">
    <Grid />
</Window>

An exception is thrown in the Window_Loaded event

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        throw new ApplicationException();
    }

However the exception is not catched by the catch around the ShowDialog call, nor does the call return. The exception is swallowed and the window still displayed.

Why does this happen and how would I go about handling an exception in the Window_Loaded event of a WPF window? Do I have to catch it in the event-handler and Dispose the window manually?

In WinForms you need to call Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)

in order to let exceptions bubble through ShowDialog calls. Is there a similar switch that needs to be set on WPF?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:38:26+00:00Added an answer on May 20, 2026 at 7:38 am

    I’ve only seen this issue on x64 machines, with code compiled with Any Cpu.
    Changing your program to compile as x84 may fix it, but I’ve had problems there myself depending on our assemblies.
    My only code suggestion is the following, and even then it’s not guaranteed to pick it up.
    Catch the exception, and re-throw it in a Background worker.

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            /// your code here...
            throw new ApplicationException();
            /// your code here...
        }
        catch (Exception ex)
        {
            if (IntPtr.Size == 8)   // 64bit machines are unable to properly throw the errors during a Page_Loaded event.
            {
                BackgroundWorker loaderExceptionWorker = new BackgroundWorker();
                loaderExceptionWorker.DoWork += ((exceptionWorkerSender, runWorkerCompletedEventArgs) => { runWorkerCompletedEventArgs.Result = runWorkerCompletedEventArgs.Argument; });
                loaderExceptionWorker.RunWorkerCompleted += ((exceptionWorkerSender, runWorkerCompletedEventArgs) => { throw (Exception)runWorkerCompletedEventArgs.Result; });
                loaderExceptionWorker.RunWorkerAsync(ex);
            }
            else
                throw;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

WPF application, uses a Window dialog to enter PIN. Normally, it looks like this:
I have a WPF application which calls OpenFileDialog.ShowDialog. While this dialog is open it
I have the following Window created in WPF. This is how it's shown: I
I am using Window.ShowDialog() method to fetch some values from the user. However, the
I have a WPF window for editing database information, which is represented using an
I have a WPF window, which contains a button called Cancel. Under ordinary circumstances
I want to make a WPF Window that behaves like a context menu. So,
The book says The WPF Button class only adds two simple concepts on top
I am using the Caliburn Micro Window Manager to display a dialog. I would
I am planning to create a insert symbol or alphabet dialog/window in WPF more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.