1 00:00:00,550 --> 00:00:02,580 Let's try a different example here. 2 00:00:03,890 --> 00:00:07,380 Let's say I'm creating a simple some function. 3 00:00:07,760 --> 00:00:12,500 Yes, I know it already exists in Python, but we'll just create our own, a simple one that takes number 4 00:00:12,500 --> 00:00:13,880 one and number two. 5 00:00:14,480 --> 00:00:18,500 And in here, we'll just return number one plus NUM two. 6 00:00:20,350 --> 00:00:25,150 Now, if we were to just simply do some one. 7 00:00:26,390 --> 00:00:30,320 And some, too, and I ran our program. 8 00:00:30,680 --> 00:00:32,630 Well, let's print it out first. 9 00:00:37,060 --> 00:00:42,130 I click, run, I get one and two because it just adds the two strings together. 10 00:00:43,200 --> 00:00:47,490 So let's just do this and click run. 11 00:00:48,920 --> 00:00:55,350 I get a type air must be stirring not and so based on what we learned, how do we handle this? 12 00:00:56,210 --> 00:01:03,950 Well, we can add a tri block in here and wrap this function or we can build it directly into our function. 13 00:01:04,250 --> 00:01:05,780 So I can say try. 14 00:01:10,150 --> 00:01:12,010 And here have an asset class. 15 00:01:13,100 --> 00:01:17,930 That says perhaps if we get a any type of air. 16 00:01:20,750 --> 00:01:22,360 Something is wrong. 17 00:01:23,490 --> 00:01:24,420 If I run this. 18 00:01:26,720 --> 00:01:28,910 I get something is wrong. 19 00:01:30,020 --> 00:01:37,910 But here's the problem, by just doing, except with no exceptions, as a programmer, I'm reading this 20 00:01:37,910 --> 00:01:44,120 and I don't really know what actually went wrong because it could be so many things. 21 00:01:45,220 --> 00:01:53,620 So a good practice is to always catch these errors based on a specific exception this way, you know 22 00:01:53,650 --> 00:01:55,880 what the areas and you can be more descriptive. 23 00:01:56,410 --> 00:02:06,040 So here we can say type error and this type error, we can just say, please enter numbers. 24 00:02:07,550 --> 00:02:11,900 So that if I run this again, please enter numbers. 25 00:02:11,930 --> 00:02:14,930 So now, all right, my error is more descriptive. 26 00:02:15,110 --> 00:02:18,050 I should fix this and make sure that I enter numbers. 27 00:02:19,100 --> 00:02:19,490 Awesome. 28 00:02:20,450 --> 00:02:28,040 Now, a common pattern when doing hair handling is to do something like this type error as and then 29 00:02:28,040 --> 00:02:29,510 doing something like a variable. 30 00:02:29,510 --> 00:02:35,720 So whatever variable we want in our case, let's just say er so I'm saying, hey, if you can type error, 31 00:02:36,230 --> 00:02:40,820 let us use error in our error message so I can say here. 32 00:02:42,810 --> 00:02:44,190 Plus, air. 33 00:02:46,070 --> 00:02:49,760 So that if I run and let's do a string here, if I run. 34 00:02:55,290 --> 00:03:00,000 I get the air printed, let me just open this up a little bit so you can see a clear. 35 00:03:01,360 --> 00:03:05,500 Look at that, it actually gives me the air that I want. 36 00:03:06,830 --> 00:03:08,870 So that I can see that on line five. 37 00:03:10,110 --> 00:03:17,430 On this line, I get a type error, unsupported operand, and I get a descriptive error of what is happening, 38 00:03:17,790 --> 00:03:21,060 so maybe I could use it in my output message. 39 00:03:21,900 --> 00:03:24,990 But what if I don't want to use this and I do run? 40 00:03:26,950 --> 00:03:31,060 All right, everything is working again if I do, plus. 41 00:03:32,190 --> 00:03:33,780 Er and I click run. 42 00:03:35,240 --> 00:03:41,150 And this is actually tricky, but it says here, during handling of the above exception, which I'm 43 00:03:41,150 --> 00:03:48,620 doing, another exception occurred, oh boy, I try to handle something and I had an error within that. 44 00:03:48,620 --> 00:03:56,300 Er, so this is sometimes tricky, but what it means is what we get out here is actually an air object. 45 00:03:56,870 --> 00:03:58,760 It's a built in exception in Python. 46 00:03:59,270 --> 00:04:01,880 You can't add in here the way I have. 47 00:04:02,030 --> 00:04:07,850 Instead we want to use an F string so I do F and then here pass it the air. 48 00:04:09,020 --> 00:04:10,670 So that now. 49 00:04:12,120 --> 00:04:13,050 If I run this. 50 00:04:14,270 --> 00:04:20,970 Again, please enter no unsupported operand types for plus ant and SDR. 51 00:04:21,530 --> 00:04:24,860 So I actually get this air available to me. 52 00:04:26,300 --> 00:04:30,380 Now, if I was to just print this, er, let's see what it looks like. 53 00:04:33,860 --> 00:04:41,000 You see, this is the part of the air that we get, so it's very, very useful if you want to give meaningful 54 00:04:41,120 --> 00:04:42,620 errors to your users. 55 00:04:43,640 --> 00:04:52,480 Another interesting thing you can do is something like this, where you wrap these errors together, 56 00:04:52,550 --> 00:04:55,550 so let's say we have zero division error as well. 57 00:04:56,910 --> 00:05:04,080 And here, we'll just say, oops, if we, for example, divide number one by number two, let's just 58 00:05:04,080 --> 00:05:11,790 run this first I get oops if I do one divided by zero and I run this. 59 00:05:13,150 --> 00:05:18,190 I also get oops, so I can handle multiple errors the same way. 60 00:05:19,280 --> 00:05:26,930 I can also say as air here and just actually print out the air and if I run this. 61 00:05:27,880 --> 00:05:32,470 I'll get division by zero, or if I do a string. 62 00:05:33,560 --> 00:05:40,760 And run this, I get the other air type, so this, again, is really useful to combine different errors 63 00:05:40,760 --> 00:05:43,280 if you want to handle them the same way. 64 00:05:44,060 --> 00:05:48,500 But what if we actually want to raise our own errors? 65 00:05:48,680 --> 00:05:53,300 That is, we want to throw our own errors maybe inside of this except luck. 66 00:05:53,310 --> 00:05:57,290 I actually want the error to display for certain types of errors. 67 00:05:57,680 --> 00:05:58,580 How could we do that? 68 00:05:59,770 --> 00:06:01,030 Let's find out in the next video.