2012-01-17 4 views
1

사실 저는 HTML 파서에서 코드를 사용합니다. 그러나 여기서 나는 그것을 시험을 위해 다시 써 넣는다.간단한 함수를 사용하는 스칼라 - VerifyError

java.lang.VerifyError: (class: $anonfun$parse$1, method: apply signature: (Ljava/lang/String;)Lscala/runtime/Nothing$;) Inconsistent stack height 0 != 3 
     at .parse(<console>:10) 
     at .<init>(<console>:10) 
     at .<clinit>(<console>) 
     at .<init>(<console>:11) 
     at .<clinit>(<console>) 
     at $print(<console>) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:601) 
     at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) 
     at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920) 
     at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) 
     at scala.tools.nsc.io.package$$anon$2.run(package.scala:25) 
     at java.lang.Thread.run(Thread.java:722) 

내 질문에 코드가 예외를 발생 않는 이유 : 전화 parse

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 text = "a.b.c.d.1321,123.f" 
    var lines = text.split("\\.") 
    return try { lines(4).replaceAll("[^0-9]", "").toInt } catch { case _ => -1 } 
} 

하지만 난 여전히 해요 : 나는 for 루프를 사용하지 않는 경우

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 
} 

, 그것은 괜찮습니다 첫 번째 경우에 대해 혼란스러워합니다.

답변

4

전체 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 
} 
+0

감사 DHG, 나는 그것을 얻었다. 여기 버그를보고했습니다 : https://issues.scala-lang.org/browse/SI-5380 –

관련 문제