UWP app: An unhandled exception is thrown on every launch

Alexander Kvitko 41 Reputation points
2023-08-15T22:45:46.1+00:00

Every time I changed the xaml markup in my UWP app, I was getting an unhandled exception and I couldn't run my app in Visual Studio 2022. Now I get this unhandled exception every time I run the app, I need to remove the app build and do rebuild in VS to launch the application. What could be wrong or where could be the error?

Developer technologies | Universal Windows Platform (UWP)
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | XAML
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,766 Reputation points
    2023-08-16T10:45:23.6733333+00:00
    Hello there,
    
    To prevent the app from crashing due to unhandled exceptions, it's a good practice to implement global exception handling. In your App.xaml.cs file, you can subscribe to the UnhandledException event to catch and handle exceptions before they crash the app.
    
    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
        this.UnhandledException += App_UnhandledException;
    }
    
    private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
    {
        // Handle the exception here, e.Exception provides the exception details
        e.Handled = true; // Set to true to indicate that the exception has been handled
    }
    
    
    Hope this resolves your Query !!
    
    --If the reply is helpful, please Upvote and Accept it as an answer--
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.