C# .NET Exceptions : Application.ThreadException
In a Windows Forms application, the windows procedure raises the Application.ThreadException event upon an unhandled exception. Subscribe to the ThreadException event to handle the unhandled exception. The subscriber is an exception handler, which prevents the application from being terminated. Do not propagate an exception caught in this manner in the unhandled exception handler. The new exception is unprotected and will likely terminate the application. After the unhandled exception handler completes, execution continues at the next message in the message pump.
Subscribe to the ThreadException event with a ThreadExceptionEventHandler delegate, which has two parameters. The object parameter is the thread object of the thread that raised the exception. The ThreadExceptionEventArg parameter of the System.Threading namespace contains the exception that was unhandled. This is the signature of the ThreadExceptionEventHandler:
ThreadExceptionEventHandler syntax:
void ThreadExceptionEventHandler(object sender, ThreadExceptionEventArgs e)
In the following code, the OnThreadException handler is added to the ThreadException event. The bthException_Click method raises an unhandled divide by zero exception. The unhandled exception is then handled in the OnThreadException method, which displays an informative message. Run the application in release mode for the expected results. Otherwise, the Visual Studio debugger intercedes the exception.
private void btnException_Click(object sender, EventArgs e)
{
int vara = 5, varb = 0;
vara /= varb;
}
private void Form1_Load(object sender, EventArgs e)
{
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnThreadException);
}
void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
Thread t = (Thread)sender;
Exception threadexception = e.Exception;
string errormessage = "Thread ID: " + t.ManagedThreadId.ToString() + " [ " + threadexception.Message + " ]";
MessageBox.Show(errormessage);
}
2 Comments »
Leave a comment
-
Archives
- February 2009 (1)
- November 2008 (6)
- October 2008 (4)
- September 2008 (13)
- August 2008 (11)
- July 2008 (29)
- June 2008 (19)
- May 2008 (8)
-
Categories
-
RSS
Entries RSS
Comments RSS


Hai shiman
Issue in Web service:
1. In web method (AddDetails) Gender & “SpouseFact field find” are CHAR Data type, I am giving value in Single character
In Flexform.But it will be displayed ERROR HTTP REQUEST ERROR #2032 stream ERROR).
2.When I enter student num and click retrieve button If there is no record, it gives error. I need to customize record not
Found message same as when i submit button click it should display record successfully added. Is there any sample code available?
Regards
ganesh
Comment by ganesh | November 14, 2008 |
Hi Ganesh,
I am afraid, I do not have any sample code of stuffs like that. Actually I have not worked with web service yet. But I think that you are not handling the exceptions. If you do, you should not get the error messages. Any way, you keep searching, I will let you know if I get anything on that. Best of luck. Sorry for the inconvenience.
/Author
Comment by shiman | November 16, 2008 |