July 2017
Intermediate to advanced
796 pages
18h 55m
English
Suppose you want to execute your code regardless of an exception being thrown or not, then you should use the finally clause. You can place it inside the try block as follows. Here is an example:
try { val f = new FileReader("data/data.txt") } catch { case ex: FileNotFoundException => println("File not found exception") } finally { println("Dude! this code always executes") }}
Now, here's the complete example of using try...catch...finally:
package com.chapter3.ScalaFPimport java.io.IOExceptionimport java.io.FileReaderimport java.io.FileNotFoundExceptionobject TryCatch { def main(args: Array[String]) { try { val f = new FileReader("data/data.txt") } catch { case ex: FileNotFoundException => println("File not found exception") case ...Read now
Unlock full access