Try, Catch, Finally

If you want to learn more, check out Scala Cookbook and Programming in Scala.

Create Some Operation That Will Cause An Exception

"Sixteen".toFloat
Name: java.lang.NumberFormatException
Message: For input string: "Sixteen"
StackTrace:   at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
  at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
  at java.lang.Float.parseFloat(Float.java:451)
  at scala.collection.immutable.StringLike$class.toFloat(StringLike.scala:280)
  at scala.collection.immutable.StringOps.toFloat(StringOps.scala:29)

Try, Catch, Finally

// Try
try {
    // The bad operation
    "Sixteen".toFloat
// Catch any problems
} catch {
    // If it is an exception, print something
    case e: Exception => println("Something went wrong")
} finally {
    // Regardless of if there is an error or not, print this
    println("We are finally done.")
}
Something went wrong
We are finally done.





()