스칼라 컴파일러는 분기가 예외를 throw하고 반환 유형의 최소 경계를 형성하는 데 사용하지 않는다고 평가할 수 있지만 throwing 코드를 메소드에서 이동하는 경우가 아니라는 것을 평가할 수 있습니다. 컴파일러는 실제로 그것이 결코 돌아 오지 않을 것이라고 확신 할 수 없다.
Break 구문의 사용법은 혼동스러워 보입니다. 이미 Break 메서드를 제공하고 있으므로 Break를 사용하지 않아도 예외를 throw하려는 경우가 아니면 직접 제공하지 않아도됩니다.
당신의 경우 Break의 사용이 불필요하다고 생각하기 때문에 몇 가지 옵션이 남아 있습니다.
1) 간단하게 실패
def myFunc(x: Int): String = {
if (x > 10) {
"I am fine"
} else {
throw new RuntimeException("Break happened")
}
}
def usemyFunc(): Unit = {
try {
println("myFunc(11) is " + myFunc(11))
println("myFunc(5) is " + myFunc(5))
} catch {
case e: Throwable => println("myFunc failed with " + e)
}
}
2) 값 또는 예외 중 하나를 반환하는 스칼라 2.10에서 사용할 수있는 시도 클래스를() 사용에 예외를 throw합니다. 가 발생한 예외가 아닌 경우 좀 더 복잡 결과
import scala.util.Try
def myFunc(x: Int): Try[String] = {
Try {
if (x > 10) {
"I am fine"
} else {
throw new RuntimeException("Break happened")
}
}
}
def useMyFunc(): Unit = {
myFunc match {
case Try.Success(s) => println("myFunc succeded with " + s)
case Try.Failure(e) => println("myFunc failed with " + e)
}
}
3)를 사용하여 결과를 검사하고 값을 사용할 수 여부를 확인하기 위해 발신자를 강제로하지만 수 있기 때문에 이전 제안 다르다 관련성이있는 경우 대신 Option 클래스를 사용할 수 있습니다. Options를 사용하여 작업하는 여러 가지 방법이 이 위대한 cheat sheet에서 서로 어떻게 관련되어 있는지 확인할 수 있습니다.
def myFunc(x: Int): Option[String] = {
if (x > 10) {
Some("I am fine") /* Some(value) creates an Option containing value */
} else {
None /* None represents an Option that has no value */
}
}
/* There are multiple ways to work with Option instances.
One of them is using pattern matching. */
def useMyFunc(): Unit = {
myFunc(10) match {
case Some(s) => println("myFunc succeded with " + s)
case None => println("myFunc failed")
}
}
/* Another one is using the map, flatMap, getOrElse, etc methods.
They usually take a function as a parameter, which is only executed
if some condition is met.
map only runs the received function if the Option contains a value,
and passes said value as a parameter to it. It then takes the result
of the function application, and creates a new Option containing it.
getOrElse checks if the Option contains a value. If it does, it is returned
directly. If it does not, then the result of the function passed to it
is returned instead.
Chaining map and getOrElse is a common idiom meaning roughly 'transform the value
contained in this Option using this code, but if there is no value, return whatever
this other piece of code returns instead'.
*/
def useMyFunc2(): Unit = {
val toPrint = myFunc(10).map{ s =>
"myFunc(10) succeded with " + s
}.getOrElse{
"myFunc(10) failed"
}
/* toPrint now contains a message to be printed, depending on whether myFunc
returned a value or not. The Scala compiler is smart enough to infer that
both code paths return String, and make toPrint a String as well. */
println(toPrint)
}
매우 첫 번째 반복이 중단 된 상황을 상상해보십시오. callMyFunc의 결과로 무엇을 반환해야합니까? –