如何检测并纠正usless的try catch块? [英] How do I detect and correct usless try catch blocks?

查看:225
本文介绍了如何检测并纠正usless的try catch块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用.net平台编者(罗斯林),以协助执行编码标准开始。



有一个问题,我有被发现和捕获无用的挣扎的try ... catch



例如:

  //想有这样的检测,并提供删除的try ... catch 

{
//做一些工作
}
赶上(异常前)
{
罚球前;
}



这将是很好也检测到代码使用的是事实罚球前; 而不仅仅是扔; 如:

 
{
//所以有些工作
}
赶上(异常前)
{
//登录错误或任何管理例外
罚球前; //< - 如何检测和提供这种
}
修复


解决方案

这样的取决于你认为什么是无用的try-catch。我假定你的意思是catch语句是尽除抛出异常没有其他的工作。



给定一个C#语法树,您所提供的代码,您可能希望找到类型的所有语法节点 CatchClauseSyntax



然后,您可以在每个寻找 StatementSyntax ,类型为 ThrowStatementSyntax 。如果有未抛出任何声明,我们假定实际工作正在这里进行。



例如:

  VAR树= CSharpSyntaxTree.ParseText(@
公共MyClass类{
的public void方法()
{
尝试{}
赶上(例外五)
{
//无用
罚球Ë;
}
试{}
赶上(例外五)
{
//有些工作
INT aVariable = 4;
罚球Ë;
}
}
}
);

//查找所有的catch子句
VAR catchClauses = tree.GetRoot()DescendantNodesAndSelf()OfType< CatchClauseSyntax>();
//看catch块
VAR catchBlocks = catchClauses.Select(N =方式> n.DescendantNodes()OfType< BlockSyntax方式>()一());
//过滤出条文,语句都只能throw语句
VAR uselessClauses = catchBlocks.Where(N => n.Statements.All(M = GT; m为ThrowStatementSyntax));


I've started using the .Net Complier Platform (Roslyn) to assist with enforcing coding standards.

One issue I'm struggling with is discovering and catching useless try...catch blocks.

For example:

// Would like to have this detected and offer to remove the try...catch
try
{
    // Do some work
}
catch(Exception ex)
{
    throw ex;
}

It would be good to also detect the fact that the code is using throw ex; rather than just throw; such as:

try
{
    // So some work
}
catch(Exception ex)
{
    // Log the error or anything to manage the exception
    throw ex;  // <-- how to detect and offer a fix for this
}

解决方案

It sort of depends on what you consider a "useless try-catch". I've assumed you mean catch statements that do no other work except throwing the exception.

Given a C# syntax tree with the code you've provided, you might want to find all the syntax nodes of type CatchClauseSyntax.

You could then look within each for StatementSyntax that is not of type ThrowStatementSyntax. If there are any statements that are not throw, we assume real work is being done here.

For example:

var tree = CSharpSyntaxTree.ParseText(@"
public class MyClass {
public void Method()
{
    try { }
    catch(Exception e)
    {
        //useless
        throw e;
    }
    try {  }
    catch(Exception e)
    {
        //Some work
        int aVariable = 4;
        throw e;
    }
}
}
");

//Finds all catch clauses
var catchClauses = tree.GetRoot().DescendantNodesAndSelf().OfType<CatchClauseSyntax>();
//Look at the catch blocks
var catchBlocks = catchClauses.Select(n => n.DescendantNodes().OfType<BlockSyntax>().First());
//Filter out the clauses where statements all are only throw statements
var uselessClauses = catchBlocks.Where(n => n.Statements.All(m => m is ThrowStatementSyntax));

这篇关于如何检测并纠正usless的try catch块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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