.NET MAUI C# "UnhandledException". I comment all my code, but project still call that "UnhandledException"

Євгеній Алексєєв 5 Reputation points
2025-02-19T22:49:57.09+00:00

p.s. I am beginner!
I could make commit to GitHub where all code is commented, But I don't want to do it. It all the same doesn't work. I leave it here: https://github.com/SaveGex/SpellCheck
If someone can explain it me such how I can understand it I would pretty thankfully.

Developer technologies | .NET | .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 6,420 Reputation points Microsoft External Staff Moderator
    2025-08-15T06:54:31.3666667+00:00

    Hello,

    I have checked out your Github repository and noticed the development is still going on.

    I also noticed you are still using .NET MAUI 8, which is out of support. I’d strongly suggest upgrading to .NET MAUI 9 or later to keep things running smoothly with the latest support and improvements.

    Regarding the issue, an unhandled exception happens when an error pops up in your app that isn’t caught by a try...catch block, which can cause a crash. Since I don’t have the specifics on the exact exception you’re seeing, I’ll walk you through some steps to track it down and fix it.

    1. Set Up a Global Exception Handler

    To catch these unhandled exceptions before they crash your app, you can add a global exception handler. This will log the error details and even let you show a friendly message to your users. For example:

    public App()
    {
        InitializeComponent();
     
        // Catch unhandled exceptions for the app domain
        AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) =>
        {
            var exception = eventArgs.ExceptionObject as Exception;
            System.Diagnostics.Debug.WriteLine($"Unhandled exception: {exception}");
            // Show a user-friendly alert
            MainPage.DisplayAlert("Error", "Something unexpected happened.", "OK");
        };
     
        // Catch unobserved task exceptions
        TaskScheduler.UnobservedTaskException += (sender, eventArgs) =>
        {
            var exception = eventArgs.Exception;
            System.Diagnostics.Debug.WriteLine($"Unobserved task exception: {exception}");
            eventArgs.SetObserved(); // Prevents crashes
        };
     
        MainPage = new AppShell();
    }
    

    2. Check the Output Window in Visual Studio

    When you’re debugging in Visual Studio, keep an eye on the Output window. It’ll show you the exception details, like the type, message, and stack trace.

    3. Add try...catch Blocks

    For parts of your code that might fail—like network calls, file operations, or parsing data—wrap them in a try...catch block.

    try
    {
        // Example: A network call that might fail
        var result = await MyWebService.GetDataAsync();
    }
    catch (System.Exception ex)
    {
        // Log the error and let the user know
        System.Diagnostics.Debug.WriteLine($"Error: {ex.Message}");
        await MainPage.DisplayAlert("Error", $"Oops, something went wrong: {ex.Message}", "OK");
    }
    

    I hope this helps you with the issue.

    References

    You could also check out these resources for more details:


Your answer

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