【发布时间】:2021-10-26 01:03:08
【问题描述】:
import scala.util.{Try, Success, Failure}
def retry[T](n: Int)(fn: => T): T = {
Try {
return fn
} match {
case Success(x) => x
case Failure(f) => {
Thread.sleep(3000)
if (n > 1) {
retry(n - 1)(fn)
} else {
//custom error handling - dont throw exception
println("error")
}
}
}
}
在编译上述重试方法时,它会返回以下错误
error: type mismatch;
found : Unit
required: T
println("error")
^
目标是在失败的情况下不抛出异常。使用上述重试功能如何实现?
【问题讨论】:
-
好吧,如果所有重试都失败了,您需要以某种方式发出信号吗?也许返回
Option[T]而不是普通的T -
我正在尝试这样的事情,如果它有效,会通知你 import scala.util.{Try, Success, Failure} def retry[T](n: Int)(fn: => T ): Option[T] = { Try { return Some(fn) } match { case Success(x) => x case Failure(f) => { Thread.sleep(3000) if (n > 1) { retry(n - 1)(fn) } else { 无 } } } }
标签: scala apache-spark exception