I have a forward only cursor. Due to some issues, fetching specific columns on some rows might throw an exception (proprie开发者_JS百科tary driver, the exception is due to encoding actually).
So how to catch such exception in this code (Scala code):
while(rs.next){
println(rs.getString("column"))
}
I've tried while(true)
with a try/catch block inside, but apparently after the exception is thrown, I can't move the cursor no matter what. The exception is thrown when calling .next()
method.
I'd use scala.util.control.Exception
. For example:
scala> import scala.util.control.Exception._
import scala.util.control.Exception._
scala> def div2(by: Int) = catching(classOf[ArithmeticException]) opt (2 / by)
div2: (by: Int)Option[Int]
scala> div2(2)
res23: Option[Int] = Some(1)
scala> div2(0)
res24: Option[Int] = None
精彩评论