【问题标题】:Searching for Catch Throw in files (regex?)在文件中搜索 Catch Throw(正则表达式?)
【发布时间】:2014-03-21 22:06:10
【问题描述】:

我必须修复一些异常处理代码,其中基本异常没有作为内部异常传递

例如:

Try
    SomeFunction()
Catch ex As Exception
    If ex.Message = somePreDefinedExceptionMessage Then
        LogErrorMsg(ex.Message)
    Else
        Throw New Exception(ex.Message) //<--- PROBLEM
End Try

如您所见,最初的异常没有继续,我需要它。我需要搜索解决方案中的所有文件并修复与上述示例类似的任何内容。我的问题是如何搜索带有 Throws 的 Catches?这样我就可以查看是否正在传递捕获的异常。

编辑: 为清楚起见,我需要找到与一般模式匹配的任何文本块:

Catch
    //bunch of crap
    Throw //anything
    //potentially more crap (should only be whitespace/newlines)
End Try

【问题讨论】:

    标签: regex search wildcard


    【解决方案1】:

    也许你这样说,你会得到你所需要的

    Try
        SomeFunction()
    Catch ex As Exception
        If ex.Message = somePreDefinedExceptionMessage Then
            LogErrorMsg(ex.Message)
        Else
            Throw ex
    End Try
    

    ?

    【讨论】:

    • 是的,我知道如何解决它,我只是不知道如何找出所有的坏情况。因此,我需要查看每个带有 Throw 的 Catch 并确保它传递了原始异常。
    • 我在这里遗漏了一些东西:您是说在您的代码中,有时搜索到的文本在内部异常中?
    • 好吧,我终于明白了!如果是VS,我认为只有一种解决方案——CTRL + F
    【解决方案2】:

    这可能会有所帮助:http://geekswithblogs.net/akraus1/archive/2010/05/29/140147.aspx

    我在 Ctrl-F 窗口中尝试了 'catch.(.\n)*?.*throw'(并选择了使用正则表达式),它找到了 catchs,然后是 throws..

    【讨论】:

      【解决方案3】:

      我在这里发现了和你类似的情况:https://stackoverflow.com/a/20109055/2136840

      调整该解决方案,这对我来说适用于您的情况:

      /(?<!End )Try(?:[^TE]+|T(?!hrow)|E(?!nd Try))*Throw.*?End Try/gs
      

      剖析你得到的正则表达式:

      (?<!End )Try   #find a Try not immediately preceded by an End
      
      (?:[^TE]+|T(?!hrow)|E(?!nd Try))*   #take every character after that that isn't a T or E or is a T or E that isn't part of "Throw" or "End Try", respectively
      
      Throw.*?End Try   #continue grabbing a Throw, any additional characters, and an End Try
      
      gs   #global - match multiple, single-line (confusing name for having the dot match newline characters)
      

      如果它到达最后一部分并且在 End Try 之前没有 Throw,它将丢弃整个块。

      所以用英语把它放在一起,你有“找到一个不紧跟在 End 前面的 Try;然后取所有后续字符,直到你得到一个 Throw,然后按该顺序进行 End Try。”

      为了测试这一点,我在 Perl 中运行了它:

      my $str = <<EOS;
          Try
              SomeFunction()
          Catch ex As Exception
              If ex.Message = somePreDefinedExceptionMessage Then
                  LogErrorMsg(ex.Message)
              Else
                  Throw New Exception(ex.Message) //<--- PROBLEM
          End Try
      
          'Other Code
      
          Try
              SomeFunction()
          Catch ex As Exception
              If ex.Message = somePreDefinedExceptionMessage Then
                  LogErrorMsg(ex.Message)
              Else
                  Throw New Exception(ex.Message) //<--- PROBLEM
          End Try
      
          Try
              SomeFunction()
          Catch ex As Exception
              If ex.Message = somePreDefinedExceptionMessage Then
                  LogErrorMsg(ex.Message)
          End Try
      
          Try
              SomeFunction()
          Catch ex As Exception
              If ex.Message = somePreDefinedExceptionMessage Then
                  LogErrorMsg(ex.Message)
          End Try
      
          'Other Code
      
          Try
              SomeFunction()
          Catch ex As Exception
              If ex.Message = somePreDefinedExceptionMessage Then
                  LogErrorMsg(ex.Message)
              Else
                  Throw New Exception(ex.Message) //<--- PROBLEM
          End Try
      
      EOS
      
      while ($str =~ /(?<!End )Try(?:[^TE]+|T(?!hrow)|E(?!nd Try))*Throw.*?End Try/gs) {
          print "Next Error: " . $& . "\r\n\r\n";
      }
      

      注意:语法高亮似乎不像 Perl 的 heredoc。

      编辑:如果您只想要 catch 块,请使用 Catch 而不是 (?&lt;!End )Try

      【讨论】:

        猜你喜欢
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多