469,898 Members | 1,611 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

ApplicationException unhandled by user code

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(fals e);
Application.ThreadException += new
ThreadExceptionEventHandler(new
ThreadExceptionHandler().ApplicationThreadExceptio n);
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.

Apr 24 '07 #1
4 15599
It seems to me that you are throwing an ApplicationException, which
apparently is not the same exception type that you have code to handle for
(ThreadException).
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"bg***@yahoo.com" wrote:
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(fals e);
Application.ThreadException += new
ThreadExceptionEventHandler(new
ThreadExceptionHandler().ApplicationThreadExceptio n);
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.

Apr 24 '07 #2
Try this:

Go to VS 2005 menu Tools | Options and then Debugging/General. Uncheck
'Enable Just my Code'.

"bg***@yahoo.com" wrote:
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(fals e);
Application.ThreadException += new
ThreadExceptionEventHandler(new
ThreadExceptionHandler().ApplicationThreadExceptio n);
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.

Apr 24 '07 #3
On 24 Apr, 13:04, Siva M <shiva...@online.excite.comwrote:
Try this:

Go to VS 2005 menu Tools | Options and then Debugging/General. Uncheck
'Enable Just my Code'.

"b...@yahoo.com" wrote:
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(fals e);
Application.ThreadException += new
ThreadExceptionEventHandler(new
ThreadExceptionHandler().ApplicationThreadExceptio n);
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.- Dölj citerad text -

- Visa citerad text -
That worked, thanks. But why did it work?

Apr 24 '07 #4
ThreadException event should occur on any unhandled exception in current
thread; example in MSDN throws ArgumentException as well.

Still I don't understand working with unhandled exceptions. In my
application I used Application.ThreadException event to display dialog box
when unhandled exception occured and it worked... until non-catched exception
was thrown from Application.Idle event handler. ThreadException event handler
was not called and standard .NET dialog box appeared advising to send report
to MS.

If I handle AppDomain.CurrentDomain.UnhandledException, this handler *is*
called when exception in Idle occurs. But it only informs about exception.
When my handler ends, standard .NET crash dialog box appears again.

The exception in Idle handler can also be caught by sorrounding
Application.Run with try-catch. But that would require some "goto" or "while"
to resume the application if I wish to continue after exception.

I cannot make head of it:)

Pepa

"Peter Bromberg [C# MVP]" wrote:
It seems to me that you are throwing an ApplicationException, which
apparently is not the same exception type that you have code to handle for
(ThreadException).
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"bg***@yahoo.com" wrote:
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(fals e);
Application.ThreadException += new
ThreadExceptionEventHandler(new
ThreadExceptionHandler().ApplicationThreadExceptio n);
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.
Jul 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Professor Frink | last post: by
2 posts views Thread by Charles | last post: by
5 posts views Thread by Simon Tamman {Uchiha Jax} | last post: by
2 posts views Thread by Bob | last post: by
reply views Thread by Autostrad | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.