c# question

De Silva KRD 1 Reputation point
2022-04-07T18:55:45.443+00:00

how to fix exception unhandled in c#

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,292 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,196 Reputation points
    2022-04-08T00:24:48.157+00:00

    First thing is to learn how to write unit test against code then when in doubt setup an unhandled exception handler which is implemented differently for different types of projects. For a Windows Form project check out my NuGet package KP.ExceptionHandling and learn how to use it in the following GitHub repository on the front page.

    Base code to implement

    using System;  
    using System.Collections.Generic;  
    using System.Diagnostics;  
    using System.Linq;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
    using ExceptionHandling;  
      
    namespace KP_ExceptionPackageTest  
    {  
        static class Program  
        {  
            /// <summary>  
            ///  The main entry point for the application.  
            /// </summary>  
            [STAThread]  
            static void Main()  
            {  
      
                Application.SetHighDpiMode(HighDpiMode.SystemAware);  
                Application.EnableVisualStyles();  
                Application.SetCompatibleTextRenderingDefault(false);  
      
                // Handling UI thread exceptions to the event.  
                Application.ThreadException += UnhandledExceptions.Application_ThreadException;  
      
                // For handling non-UI thread exceptions to the event.   
                AppDomain.CurrentDomain.UnhandledException +=  
                    UnhandledExceptions.CurrentDomain_UnhandledException;  
      
                // Indicates capturing exception has completed  
                UnhandledExceptions.OnProcessingCompletedEvent += OnProcessingCompletedEvent;  
      
                // TODO Change this to your startup form  
                Application.Run(new Form1());  
            }  
      
            private static void OnProcessingCompletedEvent()  
            {  
                var f = new AppErrorForm { Text = @"Your title goes here" };  
                f.ShowDialog();  
                Application.Exit();  
            }  
        }  
    }  
    
    0 comments No comments