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.
8,206 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
how to fix exception unhandled in c#
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();
}
}
}