FX cop规则,用于捕获空的捕获块. [英] FX cop- rule for catching empty catch block.

查看:98
本文介绍了FX cop规则,用于捕获空的捕获块.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了使用fxcop捕获空catch块的规则,但是它捕获了其中包含throw语句的块.我想编写一条规则为空catch块抛出警告消息.
任何帮助将不胜感激.

I have written a rule for catching empty catch block using fxcop.But it catches block which have throw statement in it.I want to write a rule which throws a warning message for empty catch block.
Any help would be apprecited.

public override ProblemCollection Check(Member member)
        {
            Method method = member as Method;
            bool isCatchExists = false;
           bool isThrowExists = false;
            Instruction objInstr = null;
            if (method != null)
            {
                InstructionList iList = method.Instructions;

                for (int i = 0; i < iList.Length; i++)
                {
                    if (iList[i].OpCode == OpCode._Catch)
                    {
                        isCatchExists = true;
                    }

                    if (isCatchExists)
                    {
                       
                    }


                    if (isCatchExists)
                    {
                        //If the call is not made to HandleError method. 
                        if (!isThrowExists)
                        {
                            Resolution resolu = GetResolution(new string[] { method.ToString() });
                            Problems.Add(new Problem(resolu));
                        }
                    }

                   
                }
            }
            return Problems;

        }
    }

推荐答案

我发现以下示例:
用于检测空捕获块的自定义FxCop规则 [ ^ ].
请注意以下内容(您的代码中没有抛出检查):
I found following example:
Custom FxCop rule for detecting empty catch blocks[^].
Note following (there is no throw check in your code):
public override Problem[] Check (Method method )
{

bool isCatchExists = false;
bool isThrowExists = false;

//Get all the instrections of the method.
InstructionList iList = method.Instructions ;

for(int i=0;i<ilist.length;i++)>
{
if(iListIdea.OpCode == OpCode._Catch )
{
isCatchExists = true;
}

//Checking for the existance of the catch block
if(isCatchExists)
{
      // Checking for throw 
       if(iListIdea.OpCode == OpCode.Throw)
      {
            isThrowExists = true;
      }
}
}

if(isCatchExists )
{
//If the call is not made to HandleError method.
if(!isThrowExists)
{
Problems.Add(GetResolution(method.FullName));
}
}
return ( Problems.AsArray() );

} 


该问题的核心是如何定义空".您的代码将引发问题任何捕获块,而Elina发布的代码将包含任何不包含throw *的捕获块.除了调用方法(例如,日志函数),甚至只是设置一个字段或返回值(异步处理程序中的错误条件,也许)之外什么都不做的catch块是可以接受的,并且不应该抛出样式冲突(除非您有一个样式冲突).对他们的政策).

您实际上需要检查catch块的内容(我不记得它是在catch操作码之前还是之后,我还没有玩过这些东西),看它是否符合您对"empty"的定义".我建议空"表示没有返回语句,赋值(检查以后是否使用局部变量太难了而不值得)或方法调用(实例或静态).

(*:实际上,任何具有至少一个catch且在catch后面没有抛出异常的函数.例如,这将通过:
The core of this question is how you define ''empty''. Your code will raise as an issue any catch block, and that posted by Elina, any catch block that does not contain a throw*. Catch blocks that do nothing except call a method (e.g. a log function) or even just set a field or return a value (error conditions in asynchronous handlers, perhaps) are acceptable and shouldn''t throw a style violation (unless you have a policy against them).

You need to actually inspect the contents of the catch block (I don''t remember if that is before or after the catch opcode, I haven''t played with this stuff), and see if it hits your definition of ''empty''. I recommend that ''empty'' means no return statements, assignments (checking if a local variable is later used is too hard to be worth it) or method calls (instance or static).

(*: actually, any function which has at least one catch and no throw after the catch. For example this would pass:
void ContrivedExample(){
 int a = 42;
 try { a = 5 / 0; } catch {}

 throw new ArgumentException();
}


)


这篇关于FX cop规则,用于捕获空的捕获块.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆