Programming is an interesting profession. You fail dozens or hundreds of times a day, then take a moment to celebrate a little victory. Then you move on to your next failure/success.
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 think the big thing is don’t be afraid to fail. I think in our society today, Instagram, Twitter, it’s a highlight reel. It’s all the good things. And then when you look at it, you think like, wow, when you have a rough day or your life’s not as good as that, like, you’re failing.”
“Failure is a part of life. That’s a part of building character and growing. Without failure, who would you be? I wouldn’t be up here if I hadn’t fallen thousands of times. Made mistakes.”
“We all are human, we all have weaknesses, and I think throughout this, just being able to share that and be transparent. I know when I listen to people speak and they share their weaknesses, I’m listening. Because I can resonate.”
“So I’m not perfect. I’m not Superman. I might be in the NFL, and we might have just won the Super Bowl, but, hey, we still have daily struggles, I still have daily struggles. So that's where my faith comes in, that’s where my family comes in.”
“I think when you look at a struggle in your life, just know that’s just an opportunity for your character to grow. And that’s really just been the message. Simple. If something’s going on in your life and you’re struggling? Embrace it. Because you’re growing.”
~ Nick Foles, Philadelphia Eagles quarterback, and Super Bowl champion
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:
This is a page from my book, “A Survival Guide for New Consultants”
After I sold Mission Data in 2007, I also sold most of my belongings and drove to Alaska, a state I fell in love with during several vacations many years ago. As a result of that moving process, I sold over 400 books, keeping only the 100 or so “best” books that would fit in my car.
Forced to think about keeping only ~100 books, I learned that my favorite business books are:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.6, “Scala best practice: How to use the Option/Some/None pattern.”Back to top
For a variety of reasons, including removing
null values from your Scala code, you want to use what I call the Option/Some/None pattern. Or, if you’re interested in a problem (exception) that occurred while processing code, you may want to return Try/Success/Failure from a method instead of Option/Some/None.
“How do we convince people that in programming, simplicity and clarity — in short, what mathematicians call “elegance” — are not a dispensable luxury, but a crucial matter that decides between success and failure?”
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.1, “How to open and read a text file in Scala.”Back to top
You want to open a plain-text file in Scala and process the lines in that file.Back to top
There are two primary ways to open and read a text file:Back to top