C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Handling Application Level Exceptions in Windows and Web Applications


Posted Date: 17 Jun 2008    Resource Type: Articles    Category: .NET Framework
Author: shakti singh tanwarMember Level: Diamond    
Rating: 1 out of 5Points: 12



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

}



Responses

Author: kalpana    19 Jun 2008Member Level: Bronze   Points : 1
This is a necessary thing. I really feel good by reading this Article.Thanks Sir.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Single Exception handler for entire application  .  Single Exception handler for applications  .  Handling Exceptions at Application Level  .  Application Level Exception Handling  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: OOPS Concepts in .Net to Shake your fundamentals Part 4 - Interface and Abstract Class
Previous Resource: How to save configuration settings of the assembly into the registry?
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use