Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala 2.10 or higher, how does one supply an "empty" catch block

I haven't been able to find a way to provide an empty (no op) way to complete a catch block in the following Scala code:

var autoCloseables: List[AutoCloseable] = List()
... //some code that fills the list with various java.sql.* instances; Connection, Statement, ResultSet
autoCloseables.map(try {_.close} catch {case se: SQLException => NoOp} )

I have tried to replace "NoOp" with "()", "Unit", "None", "se.getMessage()", etc. I continue to receive an error in Eclipse stating various forms of "type mismatch; found : Unit, required: AutoCloseable => ?".

I have also tried changing the final line to the below, but still receive the same warning as above:

autoCloseables.map(try {_.close} catch {case _: Throwable => } )

Any specific guidance on this would be greatly appreciated. And, I know about the ARM library. For now, please assume I am unable to use it and need a resolution framed from this particular problem formation. Thank you.

like image 222
chaotic3quilibrium Avatar asked Dec 01 '25 08:12

chaotic3quilibrium


1 Answers

import scala.util.Try

autoCloseables.map(a => Try(a.close))
like image 156
Blake Pettersson Avatar answered Dec 02 '25 21:12

Blake Pettersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!