English 中文(简体)
重新列报的未通过RegEx设定内有例外的渔获量
原标题:Find catch statements re-thrown that don t have the inner exception set via RegEx
  • 时间:2012-05-02 15:25:43
  •  标签:
  • c#
  • regex

我试图找到我们的所有法典,在新例外被推翻后不再包含原始例外作为内部例外的情况下,再保留例外。 例如:

            catch(DBApplicationException dbEx) 
        {
            BaseApplicationException bEx = new BaseApplicationException(dbEx.Message, dbEx);
            bEx.MethodName = System.Reflection.Assembly.GetExecutingAssembly().FullName + "::" + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
            bEx.Severity = ExceptionSeverityLevel;
            PublishingManager.Publish(bEx,"");
            throw bEx;
        }
        catch(Exception e) 
        {
            BaseApplicationException bEx = new BaseApplicationException(e.Message);
            bEx.MethodName = System.Reflection.Assembly.GetExecutingAssembly().FullName + "::" + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
            bEx.Severity = ExceptionSeverityLevel;
            PublishingManager.Publish(bEx,"");
            throw bEx;
        }

第一批渔获(副渔获物(DBApplicationException dbEx)被重新thrown为“基地应用接收器”,但正如你所看到的那样,它把电文称作“Ex.Message”,然后具体指明了“Inner Exception”为“dbEx”,但第二个排入部分是“无内接受”的,它只包含“e.Message”。

因此,就我的ex鱼模式而言,我只想找到一只不包含内脏例外的渔获区,现在,使用这两个集水区的收益的Im型号。

这里,我的做法如下:

catch((.|
|
)*){((.|
|
)*)Exception(((?!,).)+);((.|
|
)*)}

这里我用来检验这一设想的方法:

public static DataSet SearchUserSessions(string username, string first, string last, string startDate, string endDate) 
    {
        DataSet ds = null;
        try 
        {
            SqlParameter [] arParms = new SqlParameter[]
            {
                new SqlParameter("@UserName", username),
                new SqlParameter("@FirstName", first),
                new SqlParameter("@LastName", last),
                new SqlParameter("@SessionStart", startDate),
                new SqlParameter("@SessionEnd", endDate)
            };

            DB db = new DB();
            ds = db.ExecuteDataset(SecurityConfig.ConnectionString, CommandType.StoredProcedure, 
                SPSearchUserSessions, (DB.Provider)SecurityConfig.ConnectionProviderType, 
                arParms); 
        }
        catch(DBApplicationException dbEx) 
        {
            BaseApplicationException bEx = new BaseApplicationException(dbEx.Message, dbEx);
            bEx.MethodName = System.Reflection.Assembly.GetExecutingAssembly().FullName + "::" + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
            bEx.Severity = ExceptionSeverityLevel;
            PublishingManager.Publish(bEx,"");
            throw bEx;
        }
        catch(Exception e) 
        {
            BaseApplicationException bEx = new BaseApplicationException(e.Message);
            bEx.MethodName = System.Reflection.Assembly.GetExecutingAssembly().FullName + "::" + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
            bEx.Severity = ExceptionSeverityLevel;
            PublishingManager.Publish(bEx,"");
            throw bEx;
        }
        return ds;
    }
最佳回答

这种做法违背了你的榜样:

catch((([^)]|
|
)*))s*{(([^}]|
|
)*?)Exception(([^,](?!
))+?);(([^}]|
|
)*)}

但我同意,除非进行一次性检查,否则,这不是解决这一问题的可行办法。

问题回答

如果我理解你试图做些什么,那将是极其困难的。 主要原因是,你重新试图回到整个<条码>>(>副渔获物/代码”栏目,这意味着你必须(至少部分)将C号放在后面。 这是因为捕获区被<代码>{><>>>>>>/代码>结构指名,它们可以有其他<代码>{><>>>>>栏目。

catch (Exception e)
{
    if (condition)
    {
        doSomething();
    }    
}

为了确定渔获物的终点,必须平衡{和}符号,以确保其匹配。 这一点是可能的。 该网络与平衡小组保持平衡,但比你更复杂。

I also notice that your regex is extremely permissive. You ve used (.| | ) where you don t really want to match every character, and all your quantifiers are greedy. This bit here...

{((.|
|
)*)Exception(((?!,).)+);((.|
|
)*)}

......将实际与“<条码>、>渔获/代码”二字之间的一切相匹配。 直到最后一份<代码>>>>>>,只要在任何地方找到诸如<代码>Exception(blah)的东西。 事实上,blah 如果没有 com,甚至再说一句话,那就毫无意义。 <代码>Exception(); DoStuff();在理论上与它一致!

海事组织的最佳选择是使用视频演播室Find UsagesBaseApplicationException类别上的特征。 您可以找到整个类别的所有使用方法,然后或许可以比较一下所有使用<代码>BaseApplicationException.InnerException的情况。 如果你必须使用一种栅栏,这至少应当有99%的渔获区没有封顶<条码>{......>}区块(为了清晰而扩大):

^s*                          #Beginning of a line, possibly followed by whitespace
catch[s
]*                  #catch, possibly followed by whitespace
(w*Exceptions+w+)[s
]* #Some type of Exception declaration, possibly followed by whitespace
{                            #An opening brace
(:?(?!.InnerException)[^}])* #Anything except a closing brace, without the term `.InnerException` appearing anywhere
}                            #The closing brace of the block

正如我前面提到的那样,这将在封顶<条码>{......>>>栏目上cho。 你可以通过平衡小组解决这一问题,但我希望保持这一简单。

Sounds fiendishly difficult and hard to maintain. Can you enforce in your project that BaseApplicationException is only thrown as a "wrapper" exception? Then you can remove the constructor that takes just a string, and test the inner exception for null in the one remaining constructor.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签