DoNotCatchGeneralExceptionTypes – Application.SetUnhandledExceptionMode

Posted: 2009-03-02 in .Net

When we first started using FxCop we were a little confused about how to handle the  “DoNotCatchGeneralExceptionTypes ” Rule, more spefically is it ok to ignore this rule as long as you are at the top of the architecture chain. The typical general try catch log swallow exit approach, to gracefully notify the end user that the game is up and its time to restart.

But enter today!!!! Today while reading through my managers code (weird I know) checking that he hasn’t forgotten the basics of programming I was reminded about why being a smart ass can sometimes backfire, well sometimes 😀

Reviewing program.cs at the very first line I found a line I didn’t understand:

[csharp]

[STAThread]
static void Main(string[] args)
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
//Etc etc….
}
[/csharp]
[csharp]
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine(“An unhandied application error has occurred and the application will be closed.”);
//Etc etc….
}

[/csharp]

Basically what my manager had just shown me was away to get rid of the general high level ctach swallow kill try catch block… Fanatastic
Not only this but if you use implement different types of UnhandledExceptionMode you can even extend this down into app.settings… Fanatastic

.Net you have done it to me again.

Cheers

Choco

Update: 20090304 Choco

Found these links that are quiet informative

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/49db67c6-449c-4d2b-993b-11f264d40c8c/

and

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/f31aa36b-63cd-4846-8fe2-a17a79510a09/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s