Applications shuts down without any error message

  • Thread starter Thread starter nagar
  • Start date Start date
N

nagar

Hi,
in a .NET 2.0 application, I'm noticing that sometimes the application
simply crashes and exists without showing any message. I have the
impression this has to do with some exceptions that are generated in a
secondary thread (if I'm not mistaken .NET 2.0 shuts down a process if
an exception is generated in a secondary thread).

How can I catch all the unhandled exception is any thread so that I
can display an error message to the user and gather more information?

Thanks
Andrea
 
Hi Andrea,

Yes, starting from .Net2.0, the unhandled exception generated in worker
thread will also crash the AppDomain, while .Net1.1 CLR will just swallow
it. In this scenario, you have 2 options:
1. Use a big try...catch around all threads procedure, so that you can
catch any unhandled exceptions. Then, you may use EventLog or some other
approaches to log the exception details and terminate the proces with
Application.Exit.(It is recommended that you terminate the process)

2. You may leverage the AppDomain.UnhandledException event to monitor the
unhandled exceptions in the AppDomain and log it. There is no need for you
to terminate the process in this scenario, because
AppDomain.UnhandledException is merely a notification event, CLR will
always terminate the AppDomain. Below sample code snippet demonstrates the
logic:

void ThreadProc()
{
throw new Exception("abc");
}

private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
}

private void Form1_Load(object sender, EventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}

void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
Exception except = e.ExceptionObject as Exception;
MessageBox.Show("Application is crashing with Stack Trace below:\n"+
except.StackTrace);
}

Additionally, Jason Clark written a good article talking about the
unhandled exceptions in .Net1.1. Although, this article only talks about
.Net1.1, much of the information is still valuable for .Net2.0:
"Unexpected Errors in Managed Applications"
http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top