过早地留下一个时髦的剧本的最佳方法是什么?
groovy脚本从给定的信息文件中读取一行,然后进行一些验证工作,如果验证失败(数据不一致)脚本需要提前离开流程。然后系统将再次调用脚本以读取相同信息文件的下一行
代码示例:
read a row
try{
//make some verification here
}catch(Exception e){
logger("exception on something occurred "+e,e)
//here need to leave a groovy script prematurely
}
答案 0 :(得分:23)
只需使用System.exit(0)
。
try {
// code
} catch(Exception e) {
logger("exception on something occurred "+e,e)
System.exit(0)
}
您可以使用退出状态代码来指明您遇到问题的行。
零值表示一切正常,正值为行号。然后,您可以让您的groovy脚本将起始行作为输入参数。
这是一个天真的实现,如果一行为空,只是一个愚蠢的例外。
file = new File(args[0])
startLine = args[1].toInteger()
file.withReader { reader ->
reader.eachLine { line, count ->
try {
if (count >= startLine) {
if (line.length() == 0) throw new Exception("error")
println count + ': ' + line
}
} catch (Exception ignore) {
System.exit(count)
}
}
}
答案 1 :(得分:23)
我很确定你可以从脚本中return
。
答案 2 :(得分:2)
只需使用return:
read a row
try{
//make some verification here
}catch(Exception e){
logger("exception on something occurred "+e,e)
//here need to leave a groovy script prematurely
return
}
答案 3 :(得分:0)
使用退货 0
read a row
try{
//make some verification here
}catch(Exception e){
logger("exception on something occurred "+e,e)
return 0;
}