Because functional programming is like algebra, there are no null values or exceptions. But of course you can still have exceptions when you try to access servers that are down or files that are missing, so what can you do? This lesson demonstrates the techniques of functional error handling in Scala.
I may work on this a little more over time, but here’s a little look at what errors, checked exceptions, and unchecked exceptions look like in Java:
If you ever need to intentionally throw and catch an exception with ScalaTest, here’s an example of how to do that:
I just read a short chapter in the book Effective Java, and realized I was doing something pretty dumb in my own code by always creating my own custom exceptions instead of using other exceptions already intended to be reused in the Java API.
Here’s a little example of how exceptions work with Scala Futures, specifically looking at the
onComplete ‘Failure’ case.
In this example I start three Futures that run for different lengths of time, and the shortest-running
Future throws an exception:
Wesley Reisz shared this nice image titled, “The Rationale for Optional” (in Java).
Optional in Java is similar to
Option in Scala.