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.

[379 byte] By [ThunderRock] at [2007-12-24]
# 1

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 check

if (pobjMember == null)

return null;

// Declare a flag variable to check for Publish method found or not

bool blnPublishFound = false;

// Declare a flag variable to check for catch block found or not

bool 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 null

if (objMethod == null)

return null;

//Store the method name in the string variable

string strMethodName = objMethod.Name.Name;

// Set a intCount

int intCount = 0;

//Check if the method is null. If not, then enter the condition

if (RuleUtilities.IsEventHandling(objMethod))

{

//For loop is used to check through all the instruction inside the method

for (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 found

blnCatchFound = 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 variable

string 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 intCount

else

{

intCount++;

}

}

// If the call or callvirt is not found, then increment intCount

// Go to next statement

else

{

intCount++;

}

// Check for the opcode value _EndHandler. This indicates the end of the catch block

// If found, then break

if (objMethod.Instructions[intCounter].OpCode == OpCode._EndHandler)

{

// Check if Publish method is found. If not found, then add to problem collection

if (blnCatchFound && !blnPublishFound)

{

// Add to problem collection

Problems.Add(new Problem(GetResolution(strMethodName), strMethodName));

break;

}

// Set the flags to false again so as to start the check for other catch block

blnCatchFound = false;

blnPublishFound = false;

}

}

}

}

return Problems;

}

Cna anybody point out where the mistake is happening.

Thanks & Regards,

ThunderRock

ThunderRock at 2007-8-31 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 2

Hi ThunderRock,

You'll have to be a bit more specific in what the problem is. Just posting a huge amount of code that is formatted pretty unreadable won't give us good options to help you. Have you stepped through the code? Do you find any try catch blocks? Did you find any calls to the method you're looking for?

Regards,

Jeffrey

JeffreyvanGogh-MSFT at 2007-8-31 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 3

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.

ThunderRock at 2007-8-31 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 4

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.

DavidM.Kean-MSFT at 2007-8-31 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 5

Hi ThunderRock

What you are trying to do sounded similar to something I'm currently working on. I don't know if you have your code working by yet but I thought I'd show you how I would have coded the rule you're trying to write.


public class UseExceptionManagerInPage_Load
{
private Boolean m_catchFound;
private Boolean m_publishFound;
private int m_catchBlockLevel;

public UseExceptionManagerInPage_Load()
: base("UseExceptionManagerInPage_Load") { }

public override TargetVisibilities TargetVisibility
{
get { return TargetVisibilities.All; }
}

public override ProblemCollection Check(Member member)
{
Method method = member as Method;
if (method == null)
return null;
if (method.Name.Name != "Page_Load")
return null;

m_catchBlockLevel = 0;
m_catchFound = false;
m_publishFound = false;
this.VisitBlock(method.Body);

if (m_catchFound && !m_publishFound)
Problems.Add(new Problem(GetResolution(method.Name.Name), method));

return Problems;
}

public override void VisitCatch(CatchNode catchNode)
{
m_catchFound = true;
m_catchBlockLevel++;
base.VisitCatch(catchNode);
m_catchBlockLevel--;
}

public override void VisitMethodCall(MethodCall call)
{
if (m_catchBlockLevel > 0)
{
MemberBinding memBinding = (call.Callee as MemberBinding);
if (memBinding != null)
{
Method calledMethod = memBinding.BoundMember as Method;
if (calledMethod != null &&
calledMethod.FullName.Contains("ExceptionManager.Publish"))
{
m_publishFound = true;
}

}
}
base.VisitMethodCall(call);
}
}


Let me know if this helps or if this wasn't what you were looking for.

-Todd King

ToddKing-MSFT at 2007-8-31 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified