July 2017
Intermediate to advanced
796 pages
18h 55m
English
Scala allows you to try/catch any exception in a single block and then perform pattern matching against it using case blocks. The basic syntax of using try...catch in Scala is as follows:
try{ // your scala code should go here} catch{ case foo: FooException => handleFooException(foo) case bar: BarException => handleBarException(bar) case _: Throwable => println("Got some other kind of exception")}finally{ // your scala code should go here, such as to close a database connection }
Thus, if you throw an exception, then you need to use the try...catch block in order to handle it nicely without crashing with an internal exception message:
package com.chapter3.ScalaFPimport java.io.IOExceptionimport java.io.FileReader ...
Read now
Unlock full access