1 00:00:00,540 --> 00:00:03,000 -: So we've just created a program 2 00:00:03,000 --> 00:00:05,910 that isn't going to error out, hopefully 3 00:00:05,910 --> 00:00:09,663 at least with value errors and zero division errors. 4 00:00:11,640 --> 00:00:15,990 But sometimes errors and exceptions can be so severe 5 00:00:15,990 --> 00:00:19,320 that we do want to stop our programs from running. 6 00:00:19,320 --> 00:00:23,313 We do wanna catch them like this with the except block, but 7 00:00:23,313 --> 00:00:27,393 at the same time also stop whatever the program is doing. 8 00:00:28,230 --> 00:00:31,140 Maybe instead of print, we actually wanna display 9 00:00:31,140 --> 00:00:32,167 that red here and says, 10 00:00:32,167 --> 00:00:35,430 "Oh, something bad's happening. Stop that." 11 00:00:35,430 --> 00:00:40,290 Well, in that case, we either don't use the except block 12 00:00:40,290 --> 00:00:43,760 or in here we can say, raise ValueError. 13 00:00:46,470 --> 00:00:49,530 Now, this is rare that you'd have to do this. 14 00:00:49,530 --> 00:00:51,570 There are some specific use cases, 15 00:00:51,570 --> 00:00:54,433 but here you would just write a message saying, 16 00:00:54,433 --> 00:00:57,540 " Hey cut it out", 17 00:00:57,540 --> 00:00:59,313 so that if I run this, 18 00:01:01,110 --> 00:01:03,270 well if I type in a valid number, 19 00:01:03,270 --> 00:01:05,459 again, please enter a number. 20 00:01:05,459 --> 00:01:09,130 Okay, I'm finally done because I raise a value error 21 00:01:10,080 --> 00:01:14,190 and it throws in here into the except block. 22 00:01:14,190 --> 00:01:16,680 So I'd have to remove this. 23 00:01:16,680 --> 00:01:20,800 And if I remove this like this and hit run 24 00:01:22,110 --> 00:01:25,657 enter five and look at that, I get a value error. 25 00:01:25,657 --> 00:01:27,180 "Hey, cut it out." 26 00:01:27,180 --> 00:01:30,123 So I'm able to throw my own errors. 27 00:01:32,310 --> 00:01:33,870 So this is really useful, 28 00:01:33,870 --> 00:01:37,410 if you're creating your own library or tool 29 00:01:37,410 --> 00:01:40,117 and you wanna let the user know that an error happened 30 00:01:40,117 --> 00:01:42,367 "I'm not gonna keep doing what you're doing." 31 00:01:43,740 --> 00:01:45,900 And this could be any type of error. 32 00:01:45,900 --> 00:01:48,330 You can also do something like exception 33 00:01:48,330 --> 00:01:51,700 and if I click run here, do five 34 00:01:53,730 --> 00:01:56,217 I get an exception, "Hey, cut it out." 35 00:01:57,810 --> 00:02:01,080 The key thing to remember with errors 36 00:02:01,080 --> 00:02:05,490 is that errors are unavoidable in programming. 37 00:02:05,490 --> 00:02:09,479 What your job is as a programmer is to be able to 38 00:02:09,479 --> 00:02:13,680 anticipate these errors, these bugs, these exceptions 39 00:02:13,680 --> 00:02:17,490 and handle them properly in your programs. 40 00:02:17,490 --> 00:02:19,920 And some of the biggest errors happen when 41 00:02:19,920 --> 00:02:22,830 we're trying to accept input from a user 42 00:02:22,830 --> 00:02:24,570 not just using this input 43 00:02:24,570 --> 00:02:27,900 but functions, accepting arguments, 44 00:02:27,900 --> 00:02:32,040 classes accepting different inputs from users 45 00:02:32,040 --> 00:02:36,720 like method calls and trying to change attributes. 46 00:02:36,720 --> 00:02:38,850 When the outside world and our programs 47 00:02:38,850 --> 00:02:40,920 try to communicate with each other, 48 00:02:40,920 --> 00:02:42,630 errors are bound to happen. 49 00:02:42,630 --> 00:02:45,840 Our job as programmers is to try and code 50 00:02:45,840 --> 00:02:48,960 in a way that handles these exceptions gracefully 51 00:02:48,960 --> 00:02:51,630 and we can anticipate these errors. 52 00:02:51,630 --> 00:02:54,870 There's no such thing as a perfect program. 53 00:02:54,870 --> 00:02:57,300 However, we can try and do our best 54 00:02:57,300 --> 00:02:59,010 to learn from these errors 55 00:02:59,010 --> 00:03:02,733 and try to manage them so that our programs don't break. 56 00:03:03,630 --> 00:03:05,310 I'll see you on the next one. 57 00:03:05,310 --> 00:03:06,143 Bye-bye.