custom rule for checking the presence of ExceptionManager.Publish in Page_load
Hi all,
I'm trying to develop a custom rule which will help me check the presence of ExceptionManager.Publish in the Catch block of the Page_Load method. What I'm want is I don't want any throw statement inside the catch block, instaed I want the exception to be logged using the Exception Manager. So how do I go about it?
Anyone has any idea, please help.
Hi all,
The code which I've written for the above rule is as follows. First I checked whether the Page_Load contains any catch block. If yes then I check for the presence of ExceptionManager.Publish in the catch block. If not, I'm throwing an error. But this is not throwing any warning at the proper test condition.
public override ProblemCollection Check(Member pobjMember){
//System.Diagnostics.Debugger.Break();//Do the null checkif (pobjMember == null)return null;// Declare a flag variable to check for Publish method found or notbool blnPublishFound = false;// Declare a flag variable to check for catch block found or notbool blnCatchFound = false;// Converts type 'Microsoft.Cci.Member' to 'Microsoft.Cci.Method' // via a built in conversion Method objMethod = pobjMember as Method;//If the method is null, return nullif (objMethod == null)return null;//Store the method name in the string variablestring strMethodName = objMethod.Name.Name;// Set a intCount int intCount = 0;//Check if the method is null. If not, then enter the conditionif (RuleUtilities.IsEventHandling(objMethod)){
//For loop is used to check through all the instruction inside the methodfor (int intCounter = intCount; intCounter < objMethod.Instructions.Length; intCounter++){
//If the opcode encountered is throw or rethrow, then passed.if ((objMethod.Instructions[intCounter].OpCode == OpCode._Catch) || intCount > 0){
// Set the catch flag to true as catch block is foundblnCatchFound =
true;// Check if the opcode value is call or callvirt. If yes, then condition passed if (objMethod.Instructions[intCounter].OpCode == OpCode.Call|| objMethod.Instructions[intCounter].OpCode ==
OpCode.Callvirt){
// Store the full name of the instruction in a local variablestring strTempStore = ((Microsoft.Cci.Method)(objMethod.Instructions[intCounter].Value)).FullName;// Check if the call or callvirt has operand Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish(System.Exception) if (strTempStore.Contains("ExceptionManager.Publish")){
// Set the Publish method found flag to true.blnPublishFound =
true;}
// Else increment intCountelse{
intCount++;
}
}
// If the call or callvirt is not found, then increment intCount // Go to next statementelse{
intCount++;
}
// Check for the opcode value _EndHandler. This indicates the end of the catch block// If found, then breakif (objMethod.Instructions[intCounter].OpCode == OpCode._EndHandler){
// Check if Publish method is found. If not found, then add to problem collectionif (blnCatchFound && !blnPublishFound){
// Add to problem collectionProblems.Add(
new Problem(GetResolution(strMethodName), strMethodName));break;}
// Set the flags to false again so as to start the check for other catch blockblnCatchFound =
false;blnPublishFound =
false;}
}
}
}
return Problems;}
Cna anybody point out where the mistake is happening.
Thanks & Regards,
ThunderRock
Hi,
I've already written in the previous post what I'm trying to do. The source code which I posted was for the custom rule which will help me in checking the presence of ExceptionManager.Publish in the catch block if there is any try catch block in the Page_load method. For your information, I've already done whatever you told me but I didn't get the solution.
I got the try catch block also but the only thing it's not doing is instead of throwing a warning if no ExceptionManager.Publish is found in the catch block, it's just throwing warning for any catch block found.
Unfortunately, as we are heads down working on Visual Studio Orcas and trying to get Visual Studio 2005 SP1 out the door, it makes it hard for us to spend a lot of time on this.
Can you physically try stepping through the code using the debugger and attempt to follow the logic of the rule? This is exactly what we would have to do for us to find the problem.