Functional error handling
Handling errors is one tricky aspect of writing code. Error handling is the essential devil we simply can't do without.
The procedural world relies on returning error codes from functions, suffering from the same problem because of null checks. We could possibly miss checking. Also, the actual business logic gets lost in the tangled error handling code.
A case in point is file handling. Opening files and reading the contents, for example, could result in exceptions. As another example, the file may not exist (perhaps there is a typo in the filename), or it may not have requisite permissions.
Throwing an exception is a much better idea. The calling code cannot ignore it, at the very least it has to catch the exception.
You can, off course, use Java style try/catch blocks in Scala. As the try/catch block is an expression, you can assign its value to a variable:
scala> case class HandsUp(msg: String) extends Exception(msg) defined class HandsUp scala> def warnThem...