Scala-java.lang.VerifyError和简单函数
实际上,我在html解析器中使用我的代码。但在这里,我重写它以供测试Scala-java.lang.VerifyError和简单函数,java,function,scala,verifyerror,Java,Function,Scala,Verifyerror,实际上,我在html解析器中使用我的代码。但在这里,我重写它以供测试 def parse: Int = { var texts = Array("a.b.c.d.1321,123.f") for (text <- texts) { var lines = text.split("\\.") return try { lines(4).replaceAll("[^0-9]", "").toInt } catch { case _ => 0 } } 0 }
def parse: Int = {
var texts = Array("a.b.c.d.1321,123.f")
for (text <- texts) {
var lines = text.split("\\.")
return try { lines(4).replaceAll("[^0-9]", "").toInt } catch { case _ => 0 }
}
0
}
我的问题是,为什么代码会引发这种异常
已编辑
请不要提及编码风格,只关注例外情况。因为代码可以成功编译
已编辑
稍微改变一下结果:
def parse: Int = {
var texts = Array("a.b.c.d.1321,123.f")
for (text <- texts) {
var lines = text.split("\\.")
return try { lines(4).replaceAll("[^0-9]", "").toInt } catch { case _ => -1 }
}
0
}
但是我仍然对第一个案例感到困惑。这似乎是因为您返回的是整个
try
块的结果。如果将返回移动到块内,则可以:
try { return lines(4).replaceAll("[^0-9]", "").toInt } catch { case _ => return 0 }
它看起来像是一个编译器错误,与在for
循环中返回try
块有关。请注意,这可以正常工作:
def parse: Int = {
return try { 1 } catch { case _ => 0 }
0
}
尽管这样做失败了:
def parse: Int = {
for (x <- List(1))
return try { 1 } catch { case _ => 0 }
0
}
def解析:Int={
对于(x0}
0
}
这似乎是因为您正在返回整个try
块的结果。如果将return
移动到块内,则可以:
try { return lines(4).replaceAll("[^0-9]", "").toInt } catch { case _ => return 0 }
这看起来像是一个编译器错误,它与在for
循环中返回try
块有关。请注意,这可以正常工作:
def parse: Int = {
return try { 1 } catch { case _ => 0 }
0
}
尽管这样做失败了:
def parse: Int = {
for (x <- List(1))
return try { 1 } catch { case _ => 0 }
0
}
def解析:Int={
对于(x0}
0
}
谢谢dhg,我知道了。我在这里报告了一个错误:谢谢dhg,我知道了。我在这里报告了一个错误: