我必须修复一些异常处理代码,其中基本异常不会作为内部异常传递
例如:
Try
SomeFunction()
Catch ex As Exception
If ex.Message = somePreDefinedExceptionMessage Then
LogErrorMsg(ex.Message)
Else
Throw New Exception(ex.Message) //<--- PROBLEM
End Try正如您所看到的,最初的异常并没有继续下去,我需要它继续下去。我需要搜索解决方案中的所有文件,并修复上面示例中的任何内容。我的问题是,我如何才能搜索带有投掷的接球?这样,我就可以查看并查看是否传入了捕获的异常。
编辑:为了清晰起见,我需要找到与一般模式匹配的任何文本块:
Catch
//bunch of crap
Throw //anything
//potentially more crap (should only be whitespace/newlines)
End Try发布于 2014-03-22 06:32:02
也许你这样说,你就会得到你想要的。
Try
SomeFunction()
Catch ex As Exception
If ex.Message = somePreDefinedExceptionMessage Then
LogErrorMsg(ex.Message)
Else
Throw ex
End Try发布于 2014-03-22 07:10:38
这可能会有帮助:http://geekswithblogs.net/akraus1/archive/2010/05/29/140147.aspx
我尝试在Ctrl-F窗口中使用'catch.(.\n)*?.*throw‘(并选择Use Regular Expression),它发现catch后面紧跟着throws。
发布于 2014-03-22 08:07:13
我在这里发现了一个与您类似的情况: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)如果它到达最后一部分,并且在结束尝试之前没有抛出,它将丢弃整个块。
因此,用英语把它放在一起,你需要“找到一个没有紧跟在结尾之前的Try;然后获取所有后续字符,直到你得到一个掷球,然后按该顺序进行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而不是(?<!End )Try
https://stackoverflow.com/questions/22570363
复制相似问题