1 00:00:00,450 --> 00:00:04,290 It's important to throw unchecked exceptions to maintain quality control. 2 00:00:05,310 --> 00:00:08,600 In this lesson, you're going to learn how to throw unchecked exceptions. 3 00:00:13,030 --> 00:00:18,130 First off, follow this path in your resources and open the folder, throwing exceptions, and if you're 4 00:00:18,130 --> 00:00:21,550 missing this path, please download the updated resources from GitHub. 5 00:00:30,410 --> 00:00:35,270 OK, before we start, we're going to place every class that models and object into a package named 6 00:00:35,270 --> 00:00:36,150 models. 7 00:00:36,830 --> 00:00:41,300 The reason I'm doing this is because I want to have a clear separation between classes that model an 8 00:00:41,300 --> 00:00:44,930 object and the main class where we create objects. 9 00:00:50,150 --> 00:00:54,950 All right, now, if you allow Vasco to refactor, it's going to import the classes for you, which 10 00:00:54,950 --> 00:00:58,370 is perfect, but for learning purposes, we're going to do it ourselves. 11 00:01:04,580 --> 00:01:09,020 So inside main, we get a whole lot of errors because it doesn't recognize the employee or the store 12 00:01:09,020 --> 00:01:09,530 class. 13 00:01:09,890 --> 00:01:12,380 That's because they're in a different folder and it can't find them. 14 00:01:12,650 --> 00:01:13,990 So we need to import them. 15 00:01:14,960 --> 00:01:17,390 So go to your model classes and at the very top. 16 00:01:19,910 --> 00:01:26,090 We're going to write package models, this line specifies the folder that outclasses in. 17 00:01:35,410 --> 00:01:37,660 You know, back in Maine, we can impart the class. 18 00:01:39,620 --> 00:01:41,300 Import models, that employee. 19 00:01:46,610 --> 00:01:48,950 And we can also import models that store. 20 00:01:50,350 --> 00:01:54,820 Now, if you want to import every clause inside the model's folder, you can also write import models 21 00:01:54,820 --> 00:01:55,540 that start. 22 00:02:03,380 --> 00:02:07,230 The star basically grabs every single file inside the model's folder. 23 00:02:07,820 --> 00:02:10,039 The syntax is also available in your cheat sheet. 24 00:02:10,050 --> 00:02:13,730 But for the sake of simplicity, I'm just going to impart the classes separately. 25 00:02:17,340 --> 00:02:18,480 OK, let's run the code. 26 00:02:31,030 --> 00:02:36,970 And the store opens without any employees, the caller, the person whose programming, the main method, 27 00:02:37,180 --> 00:02:40,480 they're misusing the methods and constructor's from each model. 28 00:02:42,520 --> 00:02:48,370 This leads us to why do we throw unchecked exceptions, we throw unchecked exceptions that prevent the 29 00:02:48,370 --> 00:02:51,220 caller from misusing methods or constructor's. 30 00:02:51,880 --> 00:02:56,260 In other words, you should throw unchecked exceptions to maintain quality control. 31 00:02:59,040 --> 00:03:03,570 And the most common, unchecked exceptions that you're going to throw are a legal argument exception, 32 00:03:03,570 --> 00:03:08,880 an illegal state exception, they act as checkpoints to make sure the programmer fixes their code. 33 00:03:13,000 --> 00:03:17,710 So inside Maine, the caller's passing is null and blank values into the employee constructor, they're 34 00:03:17,710 --> 00:03:21,130 creating three employee objects with values that don't make any sense. 35 00:03:21,750 --> 00:03:23,110 We're going to visualize the runtime. 36 00:03:37,800 --> 00:03:43,770 And you can just tell how awkward this looks and that employees shouldn't have blank names and no positions, 37 00:03:44,700 --> 00:03:48,180 every employee in the store must have a concrete name and position. 38 00:03:56,560 --> 00:04:02,770 So in the constructor, we're going to check if the name or position is null or blank, if name is equal 39 00:04:02,770 --> 00:04:03,310 to null. 40 00:04:06,000 --> 00:04:07,380 Or if the name is blank. 41 00:04:11,610 --> 00:04:13,860 Or if the position that was passed in is No. 42 00:04:19,350 --> 00:04:20,490 Or if it's blank. 43 00:04:24,970 --> 00:04:27,850 Then we're going to throw a new legal argument, exception. 44 00:04:40,130 --> 00:04:45,050 And then as we cross the application, we're going to tell the caller the name or position cannot be 45 00:04:45,050 --> 00:04:45,890 Neller blank. 46 00:04:49,840 --> 00:04:55,180 This is like a checkpoint, it's going to force the caller to fix their code, we're basically telling 47 00:04:55,180 --> 00:04:59,310 the caller, hey, man, if you want to use my constructor, you better pass incorrect correct values. 48 00:04:59,830 --> 00:05:04,210 We're throwing an unchecked exception to inform the color of their badly written code. 49 00:05:07,170 --> 00:05:08,550 Attempting to run this code. 50 00:05:16,110 --> 00:05:17,400 Results in a crash. 51 00:05:27,940 --> 00:05:30,100 Stepping in, stepping out. 52 00:05:33,430 --> 00:05:34,570 And stepping back in. 53 00:05:43,190 --> 00:05:45,920 The caller gets all the way here with the values they pastan. 54 00:05:51,030 --> 00:05:56,430 But at this point, the instructor says not so fast and throws an illegal argument exception, crashing 55 00:05:56,430 --> 00:05:57,120 the application. 56 00:06:00,870 --> 00:06:06,120 And now the color ideally is going to recognize this failure as an unchecked exception because it happened 57 00:06:06,120 --> 00:06:09,630 during the run time and they'll say, oh, OK, I got to fix my code. 58 00:06:10,110 --> 00:06:13,430 In this case, we're forcing them to pass incorrect values. 59 00:06:13,950 --> 00:06:17,910 So as the caller myself, I'm going to pass in the following values. 60 00:06:22,570 --> 00:06:24,250 Paul, he's going to be the stalker. 61 00:06:28,120 --> 00:06:29,860 Nicholas, the assistant manager. 62 00:06:35,160 --> 00:06:37,200 And Damian as the manager. 63 00:06:43,130 --> 00:06:44,650 All right, we're running the code. 64 00:06:49,490 --> 00:06:50,360 And get. 65 00:06:54,180 --> 00:07:00,750 Now, should the copy constructor throw exceptions, usually no, because the source object repass again, 66 00:07:00,750 --> 00:07:03,270 it was already created with the first constructor. 67 00:07:03,540 --> 00:07:05,850 So we can assume that all of its values are good. 68 00:07:09,460 --> 00:07:12,520 Like, for example, let me make a copy of the stalker object. 69 00:07:34,810 --> 00:07:36,490 Step into the copy constructor. 70 00:07:39,730 --> 00:07:43,420 All the values are going to be good because they went through the first constructor, so we don't have 71 00:07:43,420 --> 00:07:45,370 to worry about any illegal values. 72 00:07:56,190 --> 00:08:00,720 But what if the user passes a null into the copy constructor, surely that's not legal. 73 00:08:01,830 --> 00:08:03,110 Well, let's see what happens. 74 00:08:10,170 --> 00:08:16,510 And beautiful, the copy constructor already throws a null pointer exception, which forces the caller 75 00:08:16,530 --> 00:08:21,720 to fix their code so us throwing an illegal argument exception would just be redundance. 76 00:08:28,500 --> 00:08:34,080 All right, let's talk about the illegal state exception, this one is thrown if an object calls a method 77 00:08:34,080 --> 00:08:35,400 with an illegal status. 78 00:08:35,909 --> 00:08:41,159 In other words, you need to throw an illegal state exception if an object isn't properly initialized 79 00:08:41,159 --> 00:08:42,330 before calling a method. 80 00:08:45,430 --> 00:08:47,320 In this case, I'll put a few break points here. 81 00:08:49,660 --> 00:08:50,590 We're on the debugger. 82 00:08:58,230 --> 00:09:03,210 And you can see the store object doesn't have any employees, and so we can't allow the store to open 83 00:09:03,210 --> 00:09:04,950 unless it hires enough employees. 84 00:09:05,010 --> 00:09:08,280 The store is not in a valid state to call the open method. 85 00:09:21,890 --> 00:09:25,670 So you're going to create a for loop that runs through the length of the employees field. 86 00:09:28,910 --> 00:09:30,860 And if any of the elements are No. 87 00:09:39,170 --> 00:09:43,460 We're going to throw an illegal stain exception and tell the caller whoever is calling this method. 88 00:09:46,870 --> 00:09:49,960 You must be fully staffed before opening the store. 89 00:10:11,880 --> 00:10:14,370 Attempting to run this code or result in a crash. 90 00:10:20,040 --> 00:10:25,090 The color created a store object, but didn't update the employees from the store object. 91 00:10:25,110 --> 00:10:29,330 They tried to call the open method, but they're calling it with an illegal status. 92 00:10:29,760 --> 00:10:33,120 So our checkpoint inside the open method crafted the application. 93 00:10:34,450 --> 00:10:39,370 The color is going to recognize this failure as an unchecked exception and say, OK, I got to fix my 94 00:10:39,370 --> 00:10:44,620 code in this case, we're going to call the Senate three times and update every element with an object. 95 00:11:04,350 --> 00:11:08,970 And once all the positions are filled, only then are we allowed to open the store. 96 00:11:16,870 --> 00:11:17,950 And beautiful. 97 00:11:21,430 --> 00:11:26,890 So hope you can see that each checkpoint forces the programmer to call the methods or the constructor 98 00:11:26,890 --> 00:11:27,460 properly. 99 00:11:29,450 --> 00:11:31,850 In this lesson, you learn to throw unchecked exceptions. 100 00:11:33,740 --> 00:11:38,390 You should throw exceptions to forbid the caller from misusing methods or constructor's. 101 00:11:39,760 --> 00:11:45,300 Most common contact exceptions that you will throw are a legal argument exception, an illegal state 102 00:11:45,310 --> 00:11:50,980 exception, you should throw an illegal argument exception if a constructor or method receives a legal 103 00:11:50,980 --> 00:11:51,700 values. 104 00:11:54,170 --> 00:12:00,020 You should throw an illegal exception if an object calls a method with an illegal status, if it calls 105 00:12:00,020 --> 00:12:01,460 the method at a bad time.