是否有可能打破常规关闭 [英] is it possible to break out of closure in groovy
本文介绍了是否有可能打破常规关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能是这样的:
[1,2,3 ] .each {
println(it)
if(it == 2)
break
}
解决方案
您可以抛出异常:
try {
[1,2,3] .each {
println(it)
if(it == 2)
抛出新异常(return from closure )
} catch(Exception e){}
使用findAll或grep过滤出你的列表,然后使用each。
[1,2,3 ] .findAll {it< 3} .each {println it}
is there a way to 'break' out of a groovy closure.
maybe something like this:
[1, 2, 3].each {
println(it)
if (it == 2)
break
}
解决方案
You can throw an exception:
try {
[1, 2, 3].each {
println(it)
if (it == 2)
throw new Exception("return from closure")
}
} catch (Exception e) { }
Use could also use "findAll" or "grep" to filter out your list and then use "each".
[1, 2, 3].findAll{ it < 3 }.each{ println it }
这篇关于是否有可能打破常规关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文