[英]Groovy catch block not catching MultipleCompilationErrorsException
I am trying to catch an exception ( MultipleCompilationErrorsException
) but am having a hard time doing so (actually I am trying to catch all types of errors and exceptions if that matters).我正在尝试捕获异常(
MultipleCompilationErrorsException
),但很难这样做(实际上,如果这很重要,我正在尝试捕获所有类型的错误和异常)。 Here is what I have tried:这是我尝试过的:
try {
some erroneous crap here
println("wtf! A")
} catch(Throwable all) {
println("caught!")
}
This works.这行得通。
caught!
is shown as the output.显示为 output。
try {
try some erroneous crap here
println("wtf! A")
} catch(Throwable all) {
println("caught!")
}
This code errors out with:此代码错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: /tmp/g.groovy: 2: expecting '{', found 'some' @ line 2, column 9. try some crap here ^
org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:/tmp/g.groovy:2:期待'{',发现'一些'@第2行,第9列。在这里尝试一些废话^
1 error
1 个错误
So now that I have the exception name, I tried:所以现在我有了异常名称,我尝试了:
try {
try some erroneous crap here
println("wtf! A")
} catch(MultipleCompilationErrorsException e) {
println("caught!")
}
This errors out exactly like the above:这与上面的错误完全一样:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: /tmp/g.groovy: 2: expecting '{', found 'some' @ line 2, column 9. try some crap here ^
org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:/tmp/g.groovy:2:期待'{',发现'一些'@第2行,第9列。在这里尝试一些废话^
1 error
1 个错误
Can someone tell me what I am missing?有人能告诉我我错过了什么吗? How does one catch such an error/exception?
如何捕获这样的错误/异常?
"Try Catch's" are generally used to handle exceptions that may pop up during the run time of your code. “Try Catch”通常用于处理代码运行期间可能弹出的异常。 For example, you can try to run a command that requires a certain plugin/library to be imported but if the user doesn't have the respective plugin/library, then the "catch" will handle this exception.
例如,您可以尝试运行需要导入某个插件/库的命令,但如果用户没有相应的插件/库,则“catch”将处理此异常。
In your case, it seems that you are trying to handle an actual error with the code syntax within your try block.在您的情况下,您似乎正在尝试使用 try 块中的代码语法处理实际错误。 The try block cannot run at all if the syntax is not correct (this would be the compilation error).
如果语法不正确,try 块根本无法运行(这将是编译错误)。 My best advice would be to try running what ever is inside your try block first to see if it will throw an exception and then implement a try catch block.
我最好的建议是先尝试运行 try 块中的内容,看看它是否会引发异常,然后实现 try catch 块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.