Loading
Current section: 4. Control Flow 8 exercises
solution

Errors

Transcript

00:00 Okay. Let's put on our baseball glove and no. Just kidding. We're gonna throw and catch some errors. So, right down here, I'm just gonna go ahead and, turn those on, and we'll save this.

00:09 And here we've got nothing logged and or or the result message is is nothing. But let's jump into adding our try catch. And the LLM, of course, is gonna do a super job of this. So, we'll just talk about what is generated here. So here we want to pass a raw input.

00:28 So here's our input. It's not actually a number. We're gonna pass that to number. This is gonna create a number primitive for us, but only if it's actually a number. If it's not, it's going to give us a special type of thing called not a number.

00:42 And, we can determine if a number is not a number. I know, it's ridiculous. It is. But it's great. By using this special function on number is not a number.

00:54 We pass that parsed value, the thing that converted from the string to a number, hopefully, to that and if it's not a number then we're going to throw an error saying, hey this is not a valid number. And we also are gonna be really helpful to the developers who are using our code and tell them what we did get, so that they can say, oh, yeah. You're right. That's not a number. It's literally not a number.

01:19 And then we, get our result message. Otherwise, here's that try catch block right here. In the catch case, we're going to, say, hey. We had an error and here's the error message. So if I save this, we're gonna see we got an error.

01:34 The error message was invalid number and the value was not a number. And so we did have an error and that was the resulting message. So, there you go. There are a couple other things I wanna, mention here. Let's say that we didn't care about, the error that we got, so we're just gonna say error.

01:49 You can actually remove the error right here. So that is not you don't actually need to have that. Another interesting thing is, you can also have what's called a finally block. Finally. So whether or not there was an error, I want this to, to happen.

02:06 And this is pretty interesting because, this, actually run well, I'm not even gonna bother. It has some interesting implications with, functions and returns, but we're not gonna get too far into that. Just understand you have the try. So this is a thing that could throw an error. You have a catch.

02:23 This is what happens if an error was thrown. And you have a finally. This is what happens after both of those things are done whether or not an error was thrown. And with the catch, you have the optional error that you can grab to, make use of however you like. So that is error handling.

02:40 I hope you had a good time with this one. We'll see you in the next.