As you might know a windows application runs in single threaded apartment model. This means there is a single parent thread. This parent thread can create many threads in its lifetime but the lifetime of all child threads depend on the parent thread. The moment the parent thread dies, all the children die too.
Suppose you want your application to continue running even if some exception occurs then instead of writing try catch blocks scattered across your application, you can use ThreadExceptionEventHandler delegate class. Subscribe to ThreadException event and now when exception will occur the method associated will get execute. In that method you can either handle the exception or log the exception etc.
In the Main method write
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.Run(new Form1());
}
Then use the method as in the following code sample:
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { Console.WriteLine(e.Exception.Message); }
If you want the same scenario, that is one common handler for all exceptions on your ASP.Net website, just add a global.asax file to your application. There, you will find a method Application_Error. This is the method that gets called whenever an unhandled error occurs in your application. You can try to use this handler efficiently.
void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs
}
|
| Author: kalpana 19 Jun 2008 | Member Level: Bronze Points : 1 |
This is a necessary thing. I really feel good by reading this Article.Thanks Sir.
|