Labels

Thursday, April 24, 2008

Do not catch general exception types - FxCop

Hi,

In actual ‘Do not catch general exception types’ is a valid FxCop Error under Design Rules. According to this rule below is not a full proof exception handling.

Try

{

}

catch (FileNotFoundException e)

{

Console.WriteLine( “An exception was thrown. Message was: “ + e.Message);

}

Try

{

}

catch

{

Console.WriteLine(“Some other exception has occurred”); // Catch All Block

}

To fix a violation of this rule, either below fix is allowed.

· Catch a more specific exception, or

· Re-throw the general exception as the last statement in the catch block.

Try

{

}

catch (FileNotFoundException e)

{

Console.WriteLine( “An exception was thrown. Message was: “ + e.Message);

}

Try

{

}

catch

{

Throw;

}

Reference: http://msdn2.microsoft.com/hi-in/ms182137(en-us).aspx

Thanks & Regards,

Arun Manglick || Tech Lead

No comments:

Post a Comment