Exceptions8:16 with Craig Dennis
Let's use exceptions to help control the flow of the game. We'll attack the story: As a guesser, I should be stopped from making a guess that has already been made, so that I do not waste turns.
[MUSIC] 0:00 Welcome back. 0:04 We're getting ready to put the final touches on our MVP and 0:06 we've only got a few user stories left. 0:09 So the next one up is, as a guess or I should be stopped for 0:11 making a guess that has already been made, so that I do not waste turns. 0:14 Okay, so let's think this through real quick. 0:19 So when we pass a guess into our game object if you applyGuess, 0:22 we currently just add it to hits or misses. 0:25 What we need to do before we do any of that, 0:28 is to add a check to see if it's already been guessed. 0:31 Which we can do by seeing if it's in hits or misses. 0:35 Now if it has been guessed, 0:39 we need to let the caller on applyGuess know that they did something wrong. 0:41 We should do this at the game object level, 0:45 that way any other consumer of our game logic will benefit. 0:48 Then how should we let them know do you think? 0:51 Well it's exceptional behavior. 0:56 Hey, why don't we throw an exception, and 0:58 then any caller of that method can handle that. 1:00 Let's give it a try. 1:03 All right, so let's move this prevent multiple guesses into in progress, and 1:05 let's head over to our game class. 1:10 So let's take a look here in applyGuess. 1:12 So this is probably where we wanna run our check, right? 1:17 So before anything happens, let's do it. 1:20 We wanna see if what was passed in letter here is either in hits or misses. 1:22 So why don't we lean on indexOf. 1:27 Now remember, negative one shows that it's not found, so don't try to do this? 1:29 Why don't you go try to do this, see if misses has the letter, or 1:34 hits has the letter. 1:38 Go ahead, possibly. 1:39 Okay, here's how I did it. 1:41 So if misses.indexOf has the letter, 1:43 and again that's not not found. 1:48 If it's not not found, if it's found or 1:52 hits.indexOf(letter) is not equal to 1. 1:54 So again, that pipe means or, so 2:03 if either one of those is true, then we'll throw an exception. 2:05 I'm gonna go ahead and close my if. 2:09 So we'll throw this exception, right? 2:11 The argument that they passed in here was not allowed, 2:13 this letter argument is not allowed. 2:16 It's in the illegal argument, so 2:18 that sounds like an illegal argument exception, right? 2:19 So again, that's throw and we make a new one of those, 2:22 IllegalArgumentException, and it takes a message. 2:27 So let's go ahead and say letter has already been guessed. 2:32 Awesome, so that should do it. 2:42 So now what we need to do, 2:44 is we need to see whoever calls this method in any of our code. 2:45 We wanna make sure that they're handling this exception that can come out properly. 2:48 So in workspaces, what I can do is I come up here and I can say Edit, and 2:53 I can choose Find in Files. 2:57 Brings up a little search box up here, and I'm gonna try to find applyGuess. 2:59 And so what that'll do is it will search through all of our files, and 3:04 it found one here in Prompter where we're using it. 3:06 Which we know we just wrote this code. 3:09 But I wanted to show that example of how to use workspaces to find 3:10 where these exceptions might be called. 3:14 Okay, so the Prompter is currently returning the result of game.applyGuess. 3:16 So we don't want the exception to throw out a here basically. 3:21 Let's let's catch this. 3:25 So first thing we wanna do is we want to store that in a variable. 3:26 So let's go ahead and let's say boolean isHit equals that. 3:31 And then we wanna make sure that we still return it right, 3:37 cuz the method still wants a boolean. 3:39 So we'll say return isHit. 3:41 Okay, so now we 3:45 need to try to wrap this call to applyGuess in a try catch block. 3:50 So let's do that. 3:55 So let's make a new try. 3:56 Open that up. 4:00 I'm gonna go ahead and close that. 4:00 And then immediately we'll do a catch, and 4:02 we're catching the IllegalArgumentException. 4:05 Again you can name this whatever. 4:12 I like to name them, iae, IllegalArgumentException, and 4:13 we'll close the catch there. 4:17 Okay, so let's go ahead, and I wanna move this into the try. 4:19 And in here, what we'll do is we will just go ahead and 4:26 output the message that we have our hands already. 4:29 And we're in the Prompter, so we can prompt here. 4:32 And say printIn, and we'll just say that there was an error basically, and 4:34 it'll say getMessage, right? 4:38 That was that letter that could not be found that we put in. 4:40 So now we have a little bit of a problem, and 4:42 it might not be too obvious just looking at this. 4:44 So, remember how each one of these curly braces opens up a new scope. 4:47 Well, let's look at what we're doing in this try scope here. 4:53 We're declaring a variable called isHit in this scope. 4:56 But then we're attempting to use it in an outer scope here. 5:01 So see how this is an outside, 5:04 it's in the method scope but it's not in the try scope. 5:08 The declaration is happening inside the try scope. 5:11 If you tried to compile this, you would get an unknown symbol with an arrow 5:14 pointing right here to this isHit when you try to compile it, 5:17 which is basically saying, I have no idea what you're talking about. 5:20 That's because it's declared here. 5:23 So what we need to do, is we need to declare the variable 5:24 outside of the try block, so isHit equals false. 5:28 And then we can remove this declaration and just set it. 5:33 So there's no problem with the inner scope accessing this outer scope 5:39 here at the method scope it has access, this is declaring the method scope and 5:44 inner scope has access here but it's the other way around that doesn't work, right? 5:47 We can't define something in the inner and then how the outer work. 5:50 So now let's imagine what would happen. 5:53 So if we passed in a t and then we passed in a t again, so it's gonna come in here. 5:55 It's gonna call the applyGuess, and applyGuess is gonna throw an exception, 6:00 right, cuz it's gonna find that it's been found already. 6:03 So it's in hits, so one of these is true. 6:06 This one or this one is true, and it's gonna come in here, 6:09 it's gonna throw the new exception, and then it's gonna go right into here. 6:11 Now it's not gonna return and set this to false. 6:16 Even though it already is false. 6:18 It's not gonna return here. 6:19 It's gonna go pop right into here, and then we're gonna come through. 6:20 So let's give this a run. 6:23 All right, so let's figure out what we did. 6:35 13 errors, that can feel super overwhelming. 6:37 Let's see if we can figure out what happened. 6:40 So see all of these errors is just because of this one here, unclosed string literal. 6:43 So if we go to game.java, line 15. 6:49 I forgot a quote, why didn't you say something? 6:54 All right, let's try that again. 6:58 You probably do, you're probably sitting there yelling at the screen. 7:00 Why did you put a quarter? 7:02 Okay, so here we go. 7:05 So let's guess t. 7:06 Then I'm gonna guess t again. 7:07 And bam, it says t has already been guessed, did it? 7:09 What happens if we guess a capital T? 7:14 I'm glad we tested that. 7:18 Let's go ahead. 7:22 Let's flip it over to our board. 7:23 It took it as a different guess, didn't it? 7:25 Okay, so this is done, right? 7:28 Let's move this one over, and 7:30 unfortunately we're gonna do it one step forward, and we're gonna add another one. 7:32 So it's your let's add here, let's add a new error or 7:35 a BUG as we call them in programming. 7:39 Check the teacher's notes to find out the strange reasoning behind that 7:41 name for BUG. 7:44 But I'm gonna say BUG Uppercase and 7:45 lowercase values are treated differently. 7:49 And Trello has a nice little feature here when you come in to edit these. 7:55 Let's go ahead and set the label, let's make it red so it kinda sticks out, 8:01 it's a little louder. 8:04 So there we'll see that it's red. 8:05 I'm totally glad we checked, that could have been a big problem if people were 8:07 guessing uppercase letters, we would have been treating them completely separately. 8:10 Let's fix that right after this quick break. 8:14
You need to sign up for Treehouse in order to download course files.Sign up