groovy 异常错误类型:意外中断语句

tuwxkamq  于 2022-11-21  发布在  其他
关注(0)|答案(1)|浏览(278)

问题描述:

而在Jenkins管道中用groovy编写一个循环(下面是一个最小的例子可以再现这个问题):

def workList = [1, 2, 3, 4]
workList.each{work->
    if (work == 2) {
        break
    }
}
sh 'hello world'

我得到了一个错误如下:

09:56:34  java.lang.IllegalStateException: unexpected break statement
09:56:34    at com.cloudbees.groovy.cps.impl.CallEnv.getBreakAddress(CallEnv.java:102)
09:56:34    at com.cloudbees.groovy.cps.impl.ProxyEnv.getBreakAddress(ProxyEnv.java:52)
09:56:34    at com.cloudbees.groovy.cps.impl.ProxyEnv.getBreakAddress(ProxyEnv.java:52)
09:56:34    at com.cloudbees.groovy.cps.impl.ProxyEnv.getBreakAddress(ProxyEnv.java:52)
09:56:34    at com.cloudbees.groovy.cps.impl.BreakBlock.eval(BreakBlock.java:21)
09:56:34    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
09:56:34    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
09:56:34    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
09:56:34    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
09:56:34    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
09:56:34    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
09:56:34    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
09:56:34    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:403)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:97)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:315)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:279)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
09:56:34    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
09:56:34    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
09:56:34    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
09:56:34    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
09:56:34    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
09:56:34    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
09:56:34    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
09:56:34    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
09:56:34    at java.lang.Thread.run(Thread.java:748)
09:56:34  Finished: FAILURE

我尝试过的事情
1.我看过groovy中循环语法的相关资料,break可以中断循环。但是为什么这里有错误。
1.通过搜索错误模式java.lang.IllegalStateException: unexpected break statement也没有运气

最后一个

提前感谢!

nx7onnlm

nx7onnlm1#

你不能在不抛出错误的情况下从each类型循环中中断。如果你想中断/中止循环,你可以使用类类型循环。这里是一个例子。你可以从这里阅读更多的选项。

def workList = [1, 2, 3, 4]
for (def work : workList) { 
   if (work == 2) {
        break
    }
}

相关问题