Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling in windows application c# [duplicate]

Tags:

c#

exception

In a C# windows application I have written code to display all exceptions. I tried the code below. When running in trace mode (pressing F5) it worked (I have written my UI event functions which has created the exceptions). But when I run the standalone exe it does not catch the exception. Instead it is displayed as an unhandled exception.

static void Main()
{
    try
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        MessageBox.Show(ex.StackTrace);
    }
}

Does anybody know about it?

like image 412
Muthukumar Palaniappan Avatar asked May 30 '26 18:05

Muthukumar Palaniappan


1 Answers

You better use the unhandled exception handler:

AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CatchUnhandledException);

More information on the MSDN:

  • http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx
  • http://msdn.microsoft.com/en-us/library/ms157905.aspx#Y600
  • https://blogs.msdn.com/b/tom_krueger/archive/2005/02/17/375602.aspx
like image 101
PVitt Avatar answered Jun 01 '26 08:06

PVitt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!