12 - Functional Style and try...catch...finally

A free video tutorial from Dick Wall
Scala Developer
Rating: 4.6 out of 5Instructor rating
7 courses
31,062 students
12 - Functional Style and try...catch...finally

Learn more from the full course

Scala Applied, Part 1

Introduction to programming in the Scala language. Core syntax and concepts.

05:44:10 of on-demand video • Updated August 2018

Use the REPL (the Scala Interactive Shell) for experimentation and fun
Understand the basics of Scala syntax, including val, var, def, if, while, try, for and more
Create classes, objects and instances
Define and use function literals and higher order functions
Understand the basics of Scala's type inference and how to use it
Write custom control structures to augment those built into the language
Start down the path of a more functional style of programming
Be ready for Part 2 of Stairway to Scala Applied, which will dig into some of the differences between Scala and other languages
English [Auto]
At this point, I'm going to switch over to using something we're going to see in the next module, which is the worksheet feature in entomology, just because it's a little bit quicker than watching me type all of these examples and so in intelligibly, if I switch to that now, you'll see that I have created a method called Max Squared Double that looks very similar to that example that I just showed you. And it does. This takes two parameters. Again, we have to provide the the types for these parameters because Scala and furthermore, we don't have to provide the return type, but often that's considered good practice to put that in there anyway. So we take A and B event and then we say IFA is greater than B and then this does the val squared equals eight times eight and square times two. So again, squared times two will be what the result of this particular curly brace block is. And if B is then bigger than or equal to A, then we take B times B and return the value of that squared and that's implicitly return from that block as well. So when we call it down here and you can run the R the worksheets here, you'll actually get these or you have these examples from unpacking the the projects and we'll see how to import those in the next in the next module. But when you when you've got these, you can actually make them run. So evaluate the worksheet and I'll do that here and you'll see it run through and down the side here. You can see that indeed when we call Max squared double with five and three, five is the greater one of the two. So it does five times five, which is 25, and then multiply it by two, which gives us 50 if I change this to seven. And actually let's change it the other way, let's do five and seven. So we've got B as the larger and then I save that. You should see it re-evaluate here. If not, we can force it. Maybe I've got that turned off, but you can always force it to reevaluate. And when it re-evaluates this time it comes back with eight. So that did seven times seven squared which is forty nine and then multiply that by two and that gives us ninety eight. So the another thing that Scala makes, you know, traditionally in other languages is often a statement, but in Scala is an expression is the try catch finally construct. And so this means that when we do a try with a catch and finally whatever is in the try block, if that works, that will be returned as the value. OK, so here we can do ten divided by zero now and integers that will result in an arithmetic exception. So we can catch that and we do the catch with this constructor. We say catch open curly brace and then we provide a sequence of case or a series of cases that will hopefully try and match the exceptions that we're getting. And in this example, we're going to get an arithmetic exception. So at this point, we use this rocket and we're going to see all about the rocket syntax later. But it basically means when this happens, do this. And what we use here is just a zero. So in the case of a catch, the result of this expression will be assigned to divide it and the type that's inferred will be a combination of these two. We'll see all about that type inference later. The final block is always run. But although we have a 42 at the end here, that will not affect the result. The final block is always run, but it only has a side effect. It will never affect the actual result returned from this. So if I go back to my rapport here and you'll see this, here's my example of divided. I can put the divided try A and B minus 12, and I think B was set to 12. So that will cause an arithmetic exception. I can also put a finally block in here and just say print Len R woops, this is a side effect and I can return a hundred from here and that hundred will not come back as a result of this. It will still be zero. And that's because this one failed. So arithmetic exception is the result of this and that returns is error if this is able to work. In other words, if I change this to 11, instead you'll see this get reevaluated. Now the result is ten divided by one is ten. So hopefully that makes sense to you again. There's nothing here that stops us from ignoring the return type, in which case we turn try effectively back into a statement again. But as you can see in Scala, we have the option of making things expression's and assigning them to valse, which means that we can be more functional in our programming approach. And that's generally considered to be good practice.