Exceptions7:22 with Craig Dennis
Exceptions are everywhere in Java. Let's learn to love them. Let's learn how to throw and handle exceptions in our code, so we can explain when our code is being misused.
We left our code in a pretty fragile state. 0:00 We could definitely add way too many Pez to our dispenser, and 0:03 have a big mess on our hands. 0:07 Now, luckily Java provides a way for 0:08 us to let the user of our objects know when they've done something wrong. 0:11 I'm talking about exceptions, it's possible for us to tell the consumer that 0:15 we're having problems with how they're using our object. 0:20 Now stated another way, we can let them know that an exceptional event has 0:23 occurred and it's possible for the user to be able to handle or catch that exception. 0:27 The way we do this on the usage side of things is what is known as a try 0:33 catch block. 0:37 Now, unlike what this little guy here might tell you. 0:38 They're actually is a try. 0:41 Let's go use it. 0:42 >> So Java comes with some built in exceptions and you can create your own. 0:44 Now we'll cover creating custom exceptions in a future course but for 0:48 now I'd like you just to get familiar with how they work. 0:51 So since we're dealing with a case where an argument 0:54 that was passed into our method will actually break the object. 0:57 One exception that we could use is one that's known as the illegal 1:01 argument exception. 1:04 So let's use it right so to do it. 1:05 All right, so to do it let's rework this code a little bit here. 1:06 So instead of just letting the fill happen let's go ahead and 1:10 we'll store a new variable called newAmount and we'll store the pez at 1:13 the current pez account plus the pez amount that was passed in. 1:17 Okay, now if this new amount is more than we can store. 1:21 So if this new amount is more than we can store. 1:26 All right we know that there's a max pez limit so 1:30 if it's bigger than that then we need to let them know by throwing an exception. 1:32 Now most exceptions take a message so what this looks like is you say, throw we're 1:38 gonna make a new IllegalArgument, 1:42 spelling matters here, Exception, IllegalArgumentException. 1:51 And most of them take a message which we will push here. 1:55 And will say, Too many PEZ!!!! 1:59 So what will happen is this will be thrown and it will exit the method? 2:05 So the code will have exited before it gets here so 2:09 we can safely go ahead and do this, right? 2:12 We know that this is safe. 2:17 So again we see what it would possibly be if it's more than what's allowed there, 2:19 we throw this exception. 2:25 So, let's go see what happens. 2:26 What it looks like when an exception happens. 2:28 So, let's save this. 2:30 And again, we're talking about the illegal argument of this Pez amount is legal. 2:32 Okay,so over here. 2:36 Let's go ahead and let say, after our last little loop here of testing things, 2:39 let's say dispenser.fill and 2:45 let's put a crazy number there, let's fill it with 400. 2:49 And what I want to show off is that at 2:52 the bottom here because that exception happens. 2:55 That exception is going to do what is known as bubbling, 2:59 it's going to bubble up here. 3:01 This will never happen. 3:03 This line is never going to happen, 3:05 because the exception is going to be thrown here as well. 3:06 Okay. So let's go ahead and run this and see. 3:09 So, we'll say, clear && javac. 3:13 Example.java && java Example. 3:16 All right, so you should see a pretty nasty stack trace. 3:24 So exception in the thread in 'main' too many pez. 3:30 So we tried to fill it and it blew up and see here it's showing where it happened 3:33 this is our stack trace that happen in Pez dispenser line 18. 3:37 That's where we threw the exception at. 3:40 Cool. 3:42 So instead of getting this ugly error message back and 3:43 exiting the program hard like we did. 3:46 Because this line, this will never happen, didn't happen. 3:48 The program just exited, just like the other method did. 3:51 It just exited. 3:53 We can actually wrap this in a try catch block and 3:55 this will let us handle the exception and continue. 3:58 Here, let me show you. 4:00 So we'll say try, and we're going to open up some new scope and 4:03 I'm going to close some new scope right away. 4:07 And let's go ahead and I'm going to move both of these lines 4:09 inside of that try scope, that try block there. 4:12 Okay and so what happens is it comes in here into this and it will try it and 4:18 if not it will catch anything and you can be very specific here so 4:24 we're going to say specifically I only want to catch IllegalArgumentExceptions. 4:28 Typically I name each one of these what they are there, 4:36 so IAE, illegal argument exception. 4:39 It's totally a style thing. 4:43 And what we can do is this code will run. 4:45 If something fails in here, specifically if an illegal argument happens, 4:47 it will run this code here. 4:52 So let's say, System.out.println.("Whoa there!"). 4:53 And we will, let's just show that you can get the message, right? 5:03 That catch there is gonna pass in that i a e. 5:08 The error was %S. 5:10 So what we can do is we can say iae.getMessage and 5:17 that will get the message that we passed into the constructor of that, right? 5:21 So that where ever we pass in this message Too many Pez so 5:26 we passed that in there, too many Pez. 5:29 All right, so let's run that again. 5:32 Cool, it says Whoa there the error was Too many Pez. 5:36 I forgot to put a new line there. 5:39 But what's cool is it didn't print this this will never happen because it can't. 5:41 It's not going to get past this and it's going to run this line here this code. 5:45 Awesome. 5:49 So we've successfully blocked our Pez dispenser from getting over filled. 5:50 >> Great. 5:55 Now we have a way of communicating when our object is being used incorrectly. 5:55 Throwing and catching exceptions is very common in Java. 6:00 So it's good to get yourself familiarized with them. 6:03 Some newcomers to Java will often complain about how many exceptions are used and 6:06 defined but it's part of what leads to Java's famous stability. 6:11 You can actually define that a method might throw an exception. 6:15 And then anyone who uses that method, will need to either protect against it or 6:18 throw it themselves. 6:22 We'll touch on these in upcoming courses, and 6:23 you'll encounter them in the standard Java libraries. 6:25 If you'd like to see more information on this, check out the teacher's notes. 6:28 the exception type that we just used, illegal argument exception, is called 6:31 a runtime exception and therefore we don't need to declare at the method level. 6:35 By anticipating how things might go wrong we can provide a stable 6:40 testable object that can easily integrate into existing products. 6:44 So now that we can fill and dispense our Pez dispenser, 6:49 as well as know that if people misuse it will warn them. 6:52 I think we are done with our first object. 6:55 If what I warned you about comes true and you do indeed start imagining how 6:58 everything you see in real life could be implemented as an object, 7:03 please head to the forum use it much like you would a support group. 7:06 We're all going through the same thing and 7:09 a little camaraderie can really help in situations like this. 7:11 It gets better. 7:14 All right so before we wrap up this stage and 7:16 get started on our game, let's practice throwing and catching an exception. 7:18
You need to sign up for Treehouse in order to download course files.Sign up