开发者

Catching ResultSet.next()'s exception

开发者 https://www.devze.com 2023-03-10 00:02 出处:网络
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).

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
0

精彩评论

暂无评论...
验证码 换一张
取 消