ApplicationException unhandled by user code

B

bg_ie

Hi,

I have the following Program.cs -

namespace TestFrameworkApplication
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new
ThreadExceptionEventHandler(new
ThreadExceptionHandler().ApplicationThreadException);
Application.Run(new FormMain());
}

/// <summary>
/// Handles any thread exceptions
/// </summary>
public class ThreadExceptionHandler
{
public void ApplicationThreadException(object sender,
ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "An exception
occurred:", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
}
}
}

In my Form, I attempt the following -

private void button1_Click(object sender, EventArgs e)
{
ThrowException();
}

private void ThrowException()
{
throw new ApplicationException("Monkey exception");
}

But when I click on the corresponding button, I keep getting the
message "ApplicationException unhandled by user code" from the
debugger. But once I continue the debugger, the message box pops up
with the error. What is the debugger warning me about and can I switch
off this message if I am not actually doing anything incorrect?

Thanks for your help,

Barry.
 
C

Chris Benard

Hi Barry.

If I understand what you're trying to do correctly, you want to catch
any unhandled exceptions thrown by your application so that your
program doesn't terminate or just so that you can handle the errors
before termination.

If that is what you wish to do, you may want to look at:
AppDomain.CurrentDomain.UnhandledException
In this case, you'd changer your line that reads:
Application.ThreadException += new ThreadExceptionEventHandler(new
ThreadExceptionHandler().ApplicationThreadException);
to instead read:
AppDomain.CurrentDomain.UnhandledException += new
ThreadExceptionEventHandler(new
ThreadExceptionHandler().ApplicationThreadException);

Your signature for ApplicationThreadException() would change from:
public void ApplicationThreadException(object sender,
ThreadExceptionEventArgs e)
to:
public void ApplicationThreadException(object sender,
UnhandledExceptionEventArgs e)

That should let you catch all your exceptions, and in .Net 1.1, it
will stop your program from exiting. In .Net 2.0, your program will
_still terminate_ on an unhandled exception, even if it is handled in
this way. If you'd like the old .Net 1.1 behavior, put this in your
App.config:
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="1" />
</runtime>
</configuration>

I hope this helps.

Chris Benard
MCTS Windows, Web, SQL Server 2005
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top