1 00:00:00,540 --> 00:00:03,040 Instructor: Let's try a different example here. 2 00:00:03,900 --> 00:00:07,770 Let's say I'm creating a simple sum function. 3 00:00:07,770 --> 00:00:09,750 Yes I know it already exists in Python 4 00:00:09,750 --> 00:00:11,010 but we'll just create our own. 5 00:00:11,010 --> 00:00:14,520 A simple one that takes num1 and num2 6 00:00:14,520 --> 00:00:17,940 and in here we'll just return num1 plus num2. 7 00:00:20,370 --> 00:00:23,947 Now if we were to just simply do sum1 8 00:00:26,473 --> 00:00:27,627 and sum2 and I ran our program. 9 00:00:30,690 --> 00:00:32,793 Well let's print it out first. 10 00:00:37,110 --> 00:00:38,340 I click run. 11 00:00:38,340 --> 00:00:39,420 I get one and two 12 00:00:39,420 --> 00:00:42,243 because it just adds the two strings together. 13 00:00:43,200 --> 00:00:46,230 So let's just do this 14 00:00:46,230 --> 00:00:47,643 and click run. 15 00:00:48,930 --> 00:00:50,550 I get a type error. 16 00:00:50,550 --> 00:00:52,920 Must be a string not INT. 17 00:00:52,920 --> 00:00:54,330 So based on what we learned 18 00:00:54,330 --> 00:00:56,220 how do we handle this? 19 00:00:56,220 --> 00:01:00,960 Well we can add a try block in here and wrap this function 20 00:01:00,960 --> 00:01:04,260 or we can build it directly into our function. 21 00:01:04,260 --> 00:01:05,913 So I can say try. 22 00:01:07,530 --> 00:01:12,100 This part and here have an except clause 23 00:01:13,110 --> 00:01:15,910 that says perhaps if we get a 24 00:01:17,010 --> 00:01:22,010 any type of error we'll print something is wrong. 25 00:01:23,490 --> 00:01:24,603 If I run this. 26 00:01:26,760 --> 00:01:29,073 I get something is wrong. 27 00:01:30,060 --> 00:01:31,830 But here's a problem. 28 00:01:31,830 --> 00:01:35,970 By just doing except with no exceptions. 29 00:01:35,970 --> 00:01:37,890 As a programmer, I'm reading this 30 00:01:37,890 --> 00:01:42,510 and I don't really know what actually went wrong. 31 00:01:42,510 --> 00:01:44,260 Because it could be so many things. 32 00:01:45,240 --> 00:01:49,920 So a good practice is to always catch these errors based 33 00:01:49,920 --> 00:01:52,230 on a specific exception. 34 00:01:52,230 --> 00:01:54,450 This way you know what the error is 35 00:01:54,450 --> 00:01:56,400 and you can be more descriptive. 36 00:01:56,400 --> 00:01:59,970 So here we can say type error. 37 00:01:59,970 --> 00:02:04,970 And this type error we can just say please enter numbers 38 00:02:07,595 --> 00:02:11,910 so that if I run this I get please enter numbers. 39 00:02:11,910 --> 00:02:15,120 So now, alright, my error is more descriptive. 40 00:02:15,120 --> 00:02:18,213 I should fix this and make sure that I enter numbers. 41 00:02:19,140 --> 00:02:20,460 Awesome. 42 00:02:20,460 --> 00:02:23,520 Now a common pattern when doing error handling 43 00:02:23,520 --> 00:02:24,990 is to do something like this. 44 00:02:24,990 --> 00:02:27,570 Type error as 45 00:02:27,570 --> 00:02:29,460 and then doing something like a variable. 46 00:02:29,460 --> 00:02:30,780 So whatever variable we want. 47 00:02:30,780 --> 00:02:32,790 In our case, let's just say error. 48 00:02:32,790 --> 00:02:36,210 So I'm saying, hey, if you catch type error 49 00:02:36,210 --> 00:02:39,300 let us use error in our error message. 50 00:02:39,300 --> 00:02:40,960 So I can say here 51 00:02:42,810 --> 00:02:44,403 plus error. 52 00:02:46,080 --> 00:02:49,230 So that if I run and let's do a string here. 53 00:02:49,230 --> 00:02:50,730 If I run. 54 00:02:50,730 --> 00:02:51,563 Look at that. 55 00:02:55,320 --> 00:02:57,330 I get the error printed. 56 00:02:57,330 --> 00:02:59,160 Let me just open this up a little bit 57 00:02:59,160 --> 00:03:00,360 so you can see it clear. 58 00:03:01,350 --> 00:03:02,520 Look at that. 59 00:03:02,520 --> 00:03:05,613 It actually gives me the error that I want. 60 00:03:06,840 --> 00:03:09,003 So that I can see that on line five. 61 00:03:10,170 --> 00:03:12,420 On this line I get a type error. 62 00:03:12,420 --> 00:03:14,340 Unsupported operant. 63 00:03:14,340 --> 00:03:17,790 And I get a descriptive error of what is happening. 64 00:03:17,790 --> 00:03:21,900 So maybe I could use it in my output message. 65 00:03:21,900 --> 00:03:24,030 But what if I don't want to use this? 66 00:03:24,030 --> 00:03:25,173 And I do run? 67 00:03:27,000 --> 00:03:29,790 All right, everything is working. 68 00:03:29,790 --> 00:03:33,933 Again, if I do plus error and I click run. 69 00:03:35,280 --> 00:03:37,080 And this is actually tricky 70 00:03:37,080 --> 00:03:40,650 but it says here during handling of the above exception, 71 00:03:40,650 --> 00:03:43,800 which I'm doing, another exception occurred. 72 00:03:43,800 --> 00:03:44,633 Oh boy. 73 00:03:44,633 --> 00:03:46,500 I try to handle something 74 00:03:46,500 --> 00:03:49,200 and I had an error within that error. 75 00:03:49,200 --> 00:03:51,480 So this is sometimes tricky 76 00:03:51,480 --> 00:03:53,580 but what it means is what we get 77 00:03:53,580 --> 00:03:56,880 out here is actually an error object. 78 00:03:56,880 --> 00:03:59,280 It's a built in exception in Python. 79 00:03:59,280 --> 00:04:02,070 You can't add in here the way I have. 80 00:04:02,070 --> 00:04:04,200 Instead we wanna use an F string. 81 00:04:04,200 --> 00:04:08,013 So I do F and then here pass it the error. 82 00:04:09,030 --> 00:04:10,720 So that now 83 00:04:12,150 --> 00:04:13,150 if I run this 84 00:04:14,656 --> 00:04:16,410 I get please enter number. 85 00:04:16,410 --> 00:04:21,410 Unsupported operant type S for plus INT and STR. 86 00:04:21,540 --> 00:04:24,963 So I actually get this error available to me. 87 00:04:26,280 --> 00:04:29,370 Now if I was to just print this error 88 00:04:29,370 --> 00:04:31,230 let's see what it looks like. 89 00:04:31,230 --> 00:04:32,313 If I click run. 90 00:04:33,870 --> 00:04:37,770 You see this is the part of the error that we get. 91 00:04:37,770 --> 00:04:39,330 So it's very, very useful 92 00:04:39,330 --> 00:04:42,753 if you wanna give meaningful errors to your users. 93 00:04:43,710 --> 00:04:46,200 Another interesting thing you can do 94 00:04:46,200 --> 00:04:51,200 is something like this where you wrap these errors together. 95 00:04:52,560 --> 00:04:55,713 So let's say we have zero division error as well. 96 00:04:56,940 --> 00:04:59,730 And here we'll just say, oops. 97 00:04:59,730 --> 00:05:03,510 If we, for example, divide num1 by num2. 98 00:05:03,510 --> 00:05:05,820 Let's just run this first. 99 00:05:05,820 --> 00:05:07,290 I get, oops. 100 00:05:07,290 --> 00:05:11,040 If I do one divided by zero 101 00:05:11,040 --> 00:05:14,880 and I run this, I also get, oops. 102 00:05:14,880 --> 00:05:18,303 So I can handle multiple errors the same way. 103 00:05:19,290 --> 00:05:22,620 I can also say as error here 104 00:05:22,620 --> 00:05:25,620 and just actually print out the error. 105 00:05:25,620 --> 00:05:30,270 And if I run this I'll get division by zero. 106 00:05:30,270 --> 00:05:32,500 Or if I do a string 107 00:05:33,600 --> 00:05:37,500 and run this I get the other error type. 108 00:05:37,500 --> 00:05:40,740 So this, again, is really useful to combine different errors 109 00:05:40,740 --> 00:05:44,070 if you wanna handle them the same way. 110 00:05:44,070 --> 00:05:48,660 But what if we actually wanna raise our own errors? 111 00:05:48,660 --> 00:05:51,540 That is we wanna throw our own errors. 112 00:05:51,540 --> 00:05:53,430 Maybe inside of this except block I 113 00:05:53,430 --> 00:05:55,830 actually want the error to display 114 00:05:55,830 --> 00:05:57,690 for certain types of errors. 115 00:05:57,690 --> 00:05:58,740 How could we do that? 116 00:05:59,790 --> 00:06:01,440 Let's find out in the next video.