I have added a custom rule for Error Handling is must in all the code blocks

Jul 27, 2012 at 5:09 AM

I have written the foll code to make try catch blocks mandatory in all the code blocks as following:

butthis rule is getting applied to methodsin Program.cs file and also events in Forms in windows application. CAn anyone help to restrict this rule getting applied to these class files ???

 

I have unchecked the checkboxes in stylecop for generated and designed , but still the rules are getting applied for Program.cs

[

SourceAnalyzer(typeof(CsParser))]

 

public class NLGCodingRules : SourceAnalyzer
 

{

 

public override void AnalyzeDocument(CodeDocument document)

{

 

Param.RequireNotNull(document, "document");

 

CsDocument document2 = (CsDocument)document;

 

if ((document2.RootElement != null) && !document2.RootElement.Generated)

{

document2.WalkDocument(

new CodeWalkerElementVisitor<object>(VisitElement), null, null);

}

}

 

private bool VisitElement(CsElement element, CsElement parentElement, object context)

{

 

if (element.ElementType == ElementType.Method && !element.Generated)

{

 

var method = (Method)element;

 

bool isErrorHandled = false;

 

foreach (var token in method.Tokens)

{

 

if (token.CsTokenType == CsTokenType.Try)

{

isErrorHandled =

true;

 

return true;

}

}

 

if (isErrorHandled == false)

{

 

base.AddViolation(

element,

 

"TryCatchBlockIsNecessaryForAllCodeBlocks",

 

new object[0]);

 

return true;

}

}

 

 

return true;

}

}

Jul 30, 2012 at 5:36 PM

This is functionality, not style. Such rule would be more suitable for FxCop.