1 00:00:00,850 --> 00:00:03,221 We left our code in a pretty fragile state. 2 00:00:03,221 --> 00:00:06,678 We could definitely add way too many Pez to our dispenser and 3 00:00:06,678 --> 00:00:08,870 have a big mess on our hands. 4 00:00:08,870 --> 00:00:10,742 Now, luckily, Java provides a way for 5 00:00:10,742 --> 00:00:14,280 us to let the user of our objects know when they've done something wrong. 6 00:00:15,550 --> 00:00:17,530 I'm talking about exceptions. 7 00:00:17,530 --> 00:00:21,385 It's possible for us to tell the consumer that we are having problems with how 8 00:00:21,385 --> 00:00:23,250 they're using our object. 9 00:00:23,250 --> 00:00:24,647 Now, stated another way, 10 00:00:24,647 --> 00:00:28,360 we can let them know that an exceptional event has occurred. 11 00:00:28,360 --> 00:00:33,680 And it's possible for the user to be able to handle or catch that exception. 12 00:00:33,680 --> 00:00:36,701 The way we do this on the usage side of things is what is known as 13 00:00:36,701 --> 00:00:38,110 a try/catch block. 14 00:00:38,110 --> 00:00:42,770 Now, unlike what this little guy here might tell you, there actually is a try. 15 00:00:42,770 --> 00:00:43,370 Let's go use it. 16 00:00:44,380 --> 00:00:48,170 So Java comes with some built-in exceptions and you can create your own. 17 00:00:48,170 --> 00:00:51,210 Now, we'll cover creating custom exceptions in a future course. 18 00:00:51,210 --> 00:00:54,700 But for now, I'd like you just to get familiar with how they work. 19 00:00:54,700 --> 00:00:57,691 So since we're dealing with a case where an argument that 20 00:00:57,691 --> 00:01:00,822 was passed into our method will actually break the object. 21 00:01:00,822 --> 00:01:04,676 One exception that we could use is one that's known as the illegal argument 22 00:01:04,676 --> 00:01:05,650 exception. 23 00:01:05,650 --> 00:01:07,411 So let's use it. All right, so to do it, 24 00:01:07,411 --> 00:01:10,010 let's rework this code a little bit here. 25 00:01:10,010 --> 00:01:13,543 So instead of just letting the fill happen, let's go ahead and 26 00:01:13,543 --> 00:01:16,820 we'll store a new variable called newAmount. 27 00:01:16,820 --> 00:01:21,940 And we'll store the current pezCount plus the pezAmount that was passed in. 28 00:01:21,940 --> 00:01:26,496 Okay, now, if this new amount is more than we can store, so 29 00:01:26,496 --> 00:01:30,519 if this new amount is more than we can store, right? 30 00:01:30,519 --> 00:01:31,916 We know that there's a MAX_PEZ limit. 31 00:01:31,916 --> 00:01:38,210 So if it's bigger than that, then we need to let them know by throwing an exception. 32 00:01:38,210 --> 00:01:39,920 Now, most exceptions take a message. 33 00:01:39,920 --> 00:01:45,314 So what this looks like is you say throw, and we're gonna make a new, 34 00:01:45,314 --> 00:01:50,245 sorry, throw, we're gonna make a new IllegalArgument, and 35 00:01:50,245 --> 00:01:55,664 spelling matters here, Exception, IllegalArgumentException. 36 00:01:55,664 --> 00:02:00,560 And most of them take a message, which we will push here, and 37 00:02:00,560 --> 00:02:03,350 we'll say, Too many PEZ!!!! 38 00:02:05,016 --> 00:02:09,400 So what will happen is this will be thrown and we'll exit the method. 39 00:02:09,400 --> 00:02:12,747 So the code will have exited before it gets here. 40 00:02:12,747 --> 00:02:15,820 So we can safely go ahead and do this, right? 41 00:02:17,420 --> 00:02:19,300 We know that this is safe. 42 00:02:19,300 --> 00:02:23,375 So again, we see what it would possibly be. 43 00:02:23,375 --> 00:02:26,530 If it's more than what's allowed there, we throw this exception. 44 00:02:26,530 --> 00:02:30,170 So let's go see what happens, what it looks like when an exception happens. 45 00:02:30,170 --> 00:02:30,990 So let's save this. 46 00:02:32,550 --> 00:02:35,990 And again, we're talking about the illegal argument of this pezAmount is illegal. 47 00:02:37,510 --> 00:02:42,540 Okay, so over here, let's go ahead and let's say after our last 48 00:02:42,540 --> 00:02:47,845 little loop here of testing things, let's say dispenser.fill and 49 00:02:47,845 --> 00:02:52,426 let's put a crazy number there, let's fill with 400. 50 00:02:52,426 --> 00:02:55,610 And what I wanna show off is that at the bottom here, 51 00:02:55,610 --> 00:02:59,243 because that exception happens, that exception is going 52 00:02:59,243 --> 00:03:03,440 to do what is known as bubbling, it's gonna bubble up here. 53 00:03:03,440 --> 00:03:05,040 This will never happen. 54 00:03:05,040 --> 00:03:07,890 This line is never going to happen because the exception is going 55 00:03:07,890 --> 00:03:09,063 to be thrown here as well. 56 00:03:09,063 --> 00:03:12,199 Okay, so let's go ahead and run this and see. 57 00:03:12,199 --> 00:03:17,757 So we'll say clear && javac 58 00:03:17,757 --> 00:03:24,432 Example.java && java Example. 59 00:03:24,432 --> 00:03:29,589 All right, so we should see a pretty nasty stack trace. 60 00:03:29,589 --> 00:03:33,722 So exception in thread main, Too many PEZ. 61 00:03:33,722 --> 00:03:35,769 So we tried to fill it in and it blew up, and 62 00:03:35,769 --> 00:03:37,885 see here it's showing where it happened. 63 00:03:37,885 --> 00:03:40,165 This is our stack trace that happened in PezDispenser on line 18. 64 00:03:40,165 --> 00:03:43,655 That's where we threw the exception at, cool. 65 00:03:43,655 --> 00:03:46,276 So instead of getting this ugly error message back and 66 00:03:46,276 --> 00:03:48,557 exiting the program hard like we did, right? 67 00:03:48,557 --> 00:03:52,840 Because this line, this will never happen, didn't happen, the program just exited. 68 00:03:52,840 --> 00:03:55,210 Just like the other method, it just exited. 69 00:03:55,210 --> 00:03:57,952 We can actually wrap this in a try catch block, and 70 00:03:57,952 --> 00:04:00,842 this will let us handle the exception and continue. 71 00:04:00,842 --> 00:04:01,660 Here, let me show you. 72 00:04:03,550 --> 00:04:06,382 So we'll say try, and we're going to open up some new scope and 73 00:04:06,382 --> 00:04:09,250 I'm going to close some new scope right away. 74 00:04:09,250 --> 00:04:14,360 And let's go ahead and I want to move both of these lines inside of that try scope, 75 00:04:14,360 --> 00:04:15,798 that try block there. 76 00:04:17,882 --> 00:04:23,691 Okay, and so what happens is it comes in here into this and it will try it and 77 00:04:23,691 --> 00:04:28,870 if not, it will catch anything and you can be very specific here. 78 00:04:28,870 --> 00:04:36,130 So we're going to say specifically I only want to catch IIllegalArgumentExceptions. 79 00:04:36,130 --> 00:04:39,430 Now typically I name each one of these what they are there. 80 00:04:39,430 --> 00:04:41,820 So iae, IllegalArgumentException. 81 00:04:43,150 --> 00:04:45,030 So totally a style thing. 82 00:04:45,030 --> 00:04:48,017 And what we can do is this code will run. 83 00:04:48,017 --> 00:04:52,161 If something fails in here, specifically if an illegal argument happens, 84 00:04:52,161 --> 00:04:53,660 it will run this code here. 85 00:04:53,660 --> 00:05:02,450 So let's say, we'll say System.out.printline Whoa there! 86 00:05:04,270 --> 00:05:08,320 And we will, let's just show that you can get the message, right? 87 00:05:08,320 --> 00:05:14,430 That catch there is gonna pass in that iae, 88 00:05:14,430 --> 00:05:17,153 The error was %s. 89 00:05:17,153 --> 00:05:20,556 So what we can do is we can say iae.getMessage and 90 00:05:20,556 --> 00:05:26,040 that will get the message that we passed into the constructor of that, right? 91 00:05:26,040 --> 00:05:29,364 So remember we passed in this message, Too many PEZ, so 92 00:05:29,364 --> 00:05:32,250 we pass that in there, Too many PEZ. 93 00:05:32,250 --> 00:05:33,690 All right, so let's run that again. 94 00:05:37,220 --> 00:05:39,357 Cool, as well there, the error was, Too many PEZ, 95 00:05:39,357 --> 00:05:40,680 I forgot to put a new line there. 96 00:05:41,950 --> 00:05:44,842 But what's cool is it didn't print this, this will never happen, right? 97 00:05:44,842 --> 00:05:47,314 Because it can't, it's not going to get past this, and 98 00:05:47,314 --> 00:05:49,490 it's going to run this line here this code. 99 00:05:49,490 --> 00:05:54,050 Awesome, so we've successfully blocked our Pez dispenser from getting over filled. 100 00:05:55,080 --> 00:06:00,170 Great, now we have a way of communicating when our object is being used incorrectly. 101 00:06:00,170 --> 00:06:03,057 Throwing and catching exceptions is very common in Java, so 102 00:06:03,057 --> 00:06:05,560 it's good to get yourself familiarized with them. 103 00:06:06,780 --> 00:06:11,086 Some newcomers to Java will often complain about how many exceptions are used and 104 00:06:11,086 --> 00:06:11,662 defined. 105 00:06:11,662 --> 00:06:15,660 But it's part of what leads to Java's famous stability. 106 00:06:15,660 --> 00:06:18,346 You can actually define that a method might throw in exceptions. 107 00:06:18,346 --> 00:06:22,218 And then anyone who uses that method will need to either protect against it or 108 00:06:22,218 --> 00:06:23,381 throw it themselves. 109 00:06:23,381 --> 00:06:25,218 We'll touch on these in upcoming courses and 110 00:06:25,218 --> 00:06:28,150 you'll encounter them in the standard Java libraries. 111 00:06:28,150 --> 00:06:30,820 If you'd like to see more information on this, check out the teachers notes. 112 00:06:31,850 --> 00:06:35,498 The exception type that we just used IllegalArgumentException is called 113 00:06:35,498 --> 00:06:37,438 a runtime exception, and therefore, 114 00:06:37,438 --> 00:06:40,730 we don't need to declare it at the method level. 115 00:06:40,730 --> 00:06:44,326 By anticipating how things might go wrong, we can provide a stable, 116 00:06:44,326 --> 00:06:47,930 testable object that can easily integrate into existing products. 117 00:06:49,040 --> 00:06:53,531 So, now that we can fill, and dispense our Pez dispenser, as well as know that if 118 00:06:53,531 --> 00:06:57,850 people misuse it, will warn them, I think we are done with our first object. 119 00:06:58,860 --> 00:07:02,643 If what I warned you about comes true, and you do indeed start imagining how 120 00:07:02,643 --> 00:07:06,006 everything you see in real life could be implemented as an object, 121 00:07:06,006 --> 00:07:09,670 please head to the forum, use it much like you would a support group. 122 00:07:09,670 --> 00:07:11,505 We're all going through the same thing, 123 00:07:11,505 --> 00:07:15,080 and a little comradery can really help in situations like this, it gets better. 124 00:07:16,230 --> 00:07:19,680 Make sure to check the teacher's notes and extra credit section if you're looking for 125 00:07:19,680 --> 00:07:20,350 more practice. 126 00:07:20,350 --> 00:07:22,820 Also, thanks again for your feedback and ideas. 127 00:07:22,820 --> 00:07:27,547 It really is helping us to ensure that we are building the best courses with 128 00:07:27,547 --> 00:07:29,461 the skills you need and want. 129 00:07:29,461 --> 00:07:34,222 Please do hang out in the community forums, ask questions, answer questions. 130 00:07:34,222 --> 00:07:38,604 And remember this little tidbit of wisdom from this guy's dying breath, 131 00:07:38,604 --> 00:07:40,450 pass on what you have learned. 132 00:07:41,510 --> 00:07:44,280 Thank you so much for hanging out and I hope you had as much fun as I did. 133 00:07:44,280 --> 00:07:46,500 Looking forward to seeing you very soon. 134 00:07:46,500 --> 00:07:49,245 See you at the next course, right after this exercise.