-1

I want to be able to catch every exception of my program and show it in a MessageBox, rather than the program to just say "Has stopped working".

For some reason - everytime something fails in the software - the program says stopped working. I want to be able to show it in a MessageBox, like in Visual Studio. How is it possible?

C# WinForms.

  • http://stackoverflow.com/questions/219594/net-whats-the-best-way-to-implement-a-catch-all-exceptions-handler?rq=1 – iabbott Sep 24 '13 at 06:59
  • 1
    If the exceptions are bugs then **fix your bugs so that the program does not throw anymore**. If the exceptions are due to exogenous conditions such as file not found, etc, then catch the exceptions and handle them. – Eric Lippert Sep 24 '13 at 07:02
  • @EricLippert Sometimes I have to test the code in my VPS and not my personal computer. And when exceptions occur - I want to be able to see why and where. – user2801648 Sep 24 '13 at 07:04
  • In that case you'll just have a possibly nicer looking message with no real info to help you out, you need to look into some kind of logging to tell you where/why the errors are happening – iabbott Sep 24 '13 at 07:05
  • this is actually very important issue when "debugging" release software that is being deployed. fortunately windows also logs all uncaught exceptions. message center or something. sorry for being vague, its been a while. – morishuz Sep 24 '13 at 07:10

5 Answers5

2

Subscribe to ThreadException and CurrentDomain.UnhandledException

static void Main(){
    Application.ThreadException += ApplicationThreadException;
    AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
}
static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    ShowGenericErrorMessage();
}
static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    ShowGenericErrorMessage();
}
Hossain Muctadir
  • 3,546
  • 1
  • 19
  • 33
0

The Application_Error method in Global.asax is your last Chance to catch :

protected void Application_Error(Object sender, EventArgs e)
0

try something like :

public Form1()
        {
            InitializeComponent();
            AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
        }

        private void HandleUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show("Exception {0} was thrown", e.ToString());
        }
maximbu
  • 19
  • 3
0

Try this:

try
{
\\Code block
}

catch(Exception ex)
{
\\the object ex has details about the exception use it display the error in msg box
}

Also, this link explains exception handling simply: http://www.dotnetperls.com/exception

Anandaraj
  • 161
  • 1
  • 3
  • 13
0

Crate an unhandled exception handler like below:

static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
    Exception ex = (Exception)args.ExceptionObject;

    UtilGui.LogException(ex);
}

static void ApplicationThreadUnhandledExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs args)
{
    Exception ex = (Exception)args.Exception;

    UtilGui.LogException(ex);
}

and register it in your Main method like this:

// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new ThreadExceptionEventHandler(ApplicationThreadUnhandledExceptionHandler);

// Set the unhandled exception mode to force all Windows Forms 
// errors to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomainUnhandledExceptionHandler);
gesus
  • 471
  • 1
  • 10
  • 24