How to use try catch finally in Scala

1 Answer

0 votes
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException

object Demo {
   def main(args: Array[String]): Unit = {
        try {
            val f = new FileReader("test.txt")
        } catch {
            case ex: FileNotFoundException => {
                println("FileNotFoundException")
            }
            case ex: IOException => {
                println("IOException")
            }
        } finally {
            println("finally")
        }
   }
}




/*
run:

FileNotFoundException
finally

*/

 



answered Jun 4, 2021 by avibootz

Related questions

1 answer 208 views
1 answer 174 views
174 views asked Jul 11, 2022 by avibootz
1 answer 235 views
235 views asked Nov 23, 2020 by avibootz
1 answer 416 views
2 answers 298 views
2 answers 297 views
1 answer 170 views
...