I'm trying to avoid mutables variables, but the problem is that I have to access a val that I need to initialize inside the try (it's a db operation that migth fail) and I need that var in the finally block
I tried with several alternatives:
declaring the val inside the try block
try {
val resultSet = SQL(sql).resultSet
return ColumnInfo(resultSet.getMetaData)
} catch {
case e => throw new ColumnInfoException("Error getting metadata")
} finally {
resultSet.close
}
error: not found: value resultSet
declaring the val outside the try block without initializing it
val resultSet: java.sql.ResultSet
try {
resultSet = SQL(sql).resultSet
return ColumnInfo(resultSet.getMetaData)
} catch {
case e => throw new ColumnInfoException("Error getting metadata")
} finally {
resultSet.close
}
error: only classes can have declared but undefined members
using a var, which seems to work
var resultSet: java.sql.ResultSet = null
try {
resultSet = SQL(sql).resultSet
return ColumnInfo(resultSet.getMetaData)
} catch {
case e => throw new ColumnInfoException("Error getting metadata")
} finally {
resultSet.close
}
and finally nesting try-catch block, which seems rather dirty
try {
val resultSet = SQL(sql).resultSet
try {
return ColumnInfo(resultSet.getMetaData)
} catch {
case e => throw new ColumnInfoException("Error getting metadata")
} finally {
resultSet.close
}
} catch {
case e => throw new ColumnInfoException("Error opening resultSet")
}
Is there some better approach I can take to avoid using vars and nesting try-catch blocks?
import scala.util.control.Exception._
allCatch.either(SQL(sql).resultSet).right.flatMap{ resultSet =>
val ans = allCatch.either(ColumnInfo(resultSet.getMetaData))
resultSet.close
ans
}.fold(e => throw e, identity)
or you can skip the fold
and keep the exception(s) packaged in Left
.
from this question functional try & catch w/ Scala
I learned about the loan pattern: https://wiki.scala-lang.org/display/SYGN/Loan
Play framework itself seems to use it with the DB.withConnection method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With