1 00:00:00,000 --> 00:00:00,800 [no audio] 2 00:00:00,800 --> 00:00:05,600 Friends, here we are going to see how to create custom exceptions. 3 00:00:06,500 --> 00:00:09,600 First of all, what is a custom exception? 4 00:00:11,100 --> 00:00:17,700 Simple, wantedly if you want to raise an exception, then you 5 00:00:17,700 --> 00:00:22,600 can raise. So that type of exceptions are called custom exceptions. 6 00:00:23,700 --> 00:00:28,800 And you can create this custom exceptions in two ways - 7 00:00:28,800 --> 00:00:30,900 by using 'raise' and 'assert'. 8 00:00:32,000 --> 00:00:38,300 So 'raise' we are going to use to raise an exception 9 00:00:38,400 --> 00:00:40,300 which are existing with your Python. 10 00:00:41,700 --> 00:00:44,400 Right. See let me explain this with an example. 11 00:00:45,100 --> 00:00:48,300 So I am going to save my script name as something like 12 00:00:49,400 --> 00:00:58,400 'custom_exceptions', right. See simply I am using a 'raise' 13 00:00:58,400 --> 00:01:00,900 [no audio] 14 00:01:00,900 --> 00:01:05,800 'Exception("This is a exception"). That's it. 15 00:01:06,099 --> 00:01:08,000 Now see the output. If I run this 16 00:01:09,400 --> 00:01:13,700 it is giving like an exception, right. See what is the exception 17 00:01:13,700 --> 00:01:15,300 name? "Exception" itself. 18 00:01:15,400 --> 00:01:16,800 And what is the error? 19 00:01:16,800 --> 00:01:21,500 "This is a Exception". Not only that, suppose if any variable 20 00:01:21,500 --> 00:01:26,200 is not defined you know, right. "NameError" is the exception. 21 00:01:26,400 --> 00:01:30,400 So, "Variable is not defined". Wantedly 22 00:01:30,400 --> 00:01:31,400 I'm raising this. 23 00:01:31,400 --> 00:01:37,800 [no audio] 24 00:01:37,800 --> 00:01:38,800 That's it. 25 00:01:39,600 --> 00:01:43,100 But this is not the way to use in your real-time. Suppose, 26 00:01:43,100 --> 00:01:45,300 just assume that you are trying to read a variable. 27 00:01:45,500 --> 00:01:49,700 It may be any variable. Let's say simply 'age=' some 23. 28 00:01:50,600 --> 00:01:56,300 So what I want to do is, 'if age > 30:' then 29 00:01:56,300 --> 00:01:57,300 I don't have any problem. 30 00:01:58,700 --> 00:02:02,200 'print("Valid age")'. 31 00:02:06,400 --> 00:02:09,800 But if 'age' is not less than, I mean not greater 32 00:02:09,800 --> 00:02:12,500 than 30 then I want to raise an exception. 33 00:02:14,199 --> 00:02:16,900 Instead of printing simply a message wantedly now, actually 34 00:02:16,900 --> 00:02:21,800 previously we have studied to avoid exceptions, 35 00:02:22,100 --> 00:02:26,000 but now we are trying to learn how to create exceptions if 36 00:02:26,000 --> 00:02:27,100 it is required. 37 00:02:28,300 --> 00:02:32,800 Right. Suppose if age is less than 30 then I don't want to 38 00:02:32,800 --> 00:02:34,800 run my script. I want to terminate my script. 39 00:02:34,800 --> 00:02:37,300 I want to stop. I need to raise an exception. 40 00:02:37,300 --> 00:02:39,500 Yes, you can raise, simply raise. 41 00:02:41,200 --> 00:02:44,300 So I am taking suppose, 'ValueError' exception. 42 00:02:45,900 --> 00:02:49,900 Right. So see I am using existing keywords, 'ValueError' that 43 00:02:49,900 --> 00:02:52,000 is already there with your exceptions, right? 44 00:02:52,700 --> 00:02:58,400 So that's why 'raise' keyword is used to raise an existing exception wantedly. 45 00:03:01,800 --> 00:03:05,600 "Age is less than 30". 46 00:03:06,800 --> 00:03:07,800 That's it. 47 00:03:09,300 --> 00:03:13,900 This is the way how we can raise a custom exception using 48 00:03:13,900 --> 00:03:14,900 'raise' keyword. 49 00:03:16,600 --> 00:03:19,200 Now we have one more thing called 'assert'. 50 00:03:20,200 --> 00:03:25,100 So 'assert' is going to be used to create your own exceptions with 51 00:03:25,100 --> 00:03:28,200 a name called 'AssertError'. First 52 00:03:28,200 --> 00:03:29,200 let me do that. 53 00:03:29,800 --> 00:03:32,100 See I am writing simply 'assert'. 54 00:03:33,100 --> 00:03:37,700 See, first of all how your 'assert' is going to work, 'assert' 55 00:03:37,700 --> 00:03:42,500 will raise assertion error, if the condition becomes false. 56 00:03:43,300 --> 00:03:48,300 I mean suppose I am writing 4 < 3, that is false. 57 00:03:48,400 --> 00:03:53,000 But if I write 4 > 3, that is correct, right. Now 58 00:03:53,000 --> 00:03:53,800 see that I am running, 59 00:03:53,800 --> 00:03:57,300 [no audio] 60 00:03:57,300 --> 00:03:59,900 it is not raising any exception because condition is true, 61 00:03:59,900 --> 00:04:04,400 right. If I write condition as false, now see the result what 62 00:04:04,400 --> 00:04:08,100 you are getting. You are getting error called, exception 63 00:04:08,100 --> 00:04:10,400 called "AssertionError". That's it. 64 00:04:11,200 --> 00:04:15,300 So 'assert' is used to create "AssertionError", and whenever 65 00:04:15,300 --> 00:04:20,300 if you get any "AssertionError" you have to assume that 66 00:04:20,300 --> 00:04:22,300 wantedly you created 67 00:04:23,200 --> 00:04:26,100 an exception, that's it. 68 00:04:26,100 --> 00:04:29,000 [no audio] 69 00:04:29,000 --> 00:04:31,700 Right. So here also you can take something like, suppose 70 00:04:31,700 --> 00:04:34,500 [no audio] 71 00:04:34,500 --> 00:04:38,500 if instead of 'if' condition, now directly you can give it. So 72 00:04:38,500 --> 00:04:43,200 suppose I am trying to write 'try:', actually assume that I am 73 00:04:43,200 --> 00:04:46,000 writing here suppose 'age=30'. 74 00:04:47,100 --> 00:04:51,100 Right. So now I no need to use 'if' condition. Directly if I want 75 00:04:51,100 --> 00:04:55,900 to raise an exception based on 'age' then I no need to use 'if' 76 00:04:55,900 --> 00:05:01,000 condition, 'assert' itself is going to work as 'assert', 77 00:05:02,200 --> 00:05:06,200 if 'age < 30', then only create, 78 00:05:07,400 --> 00:05:11,500 create an exception. If it is true then see, 79 00:05:11,500 --> 00:05:13,400 [no audio] 80 00:05:13,400 --> 00:05:14,400 "Valid age". 81 00:05:16,000 --> 00:05:17,500 Let me run it and see the result. 82 00:05:17,500 --> 00:05:19,000 Yeah, this is 'try' and 'except', right? 83 00:05:19,000 --> 00:05:22,100 You need to write your 'except' block as well. 84 00:05:22,500 --> 00:05:24,200 Let me write 'except:'. 85 00:05:28,300 --> 00:05:32,800 'print("Exception 86 00:05:36,800 --> 00:05:39,500 occurred")', right. 87 00:05:39,500 --> 00:05:42,000 So what type of assertion, sorry what type of exception 88 00:05:42,000 --> 00:05:44,900 you're creating with the help of 'assert', 'AssertionError'. 89 00:05:44,900 --> 00:05:46,400 Let me run it and see the result. 90 00:05:46,400 --> 00:05:49,600 [no audio] 91 00:05:49,600 --> 00:05:54,100 See both are same. Let me take 33, then condition will become true, right? 92 00:05:55,100 --> 00:06:04,200 I mean, now see what you are getting, if age is, if it is less than 30 93 00:06:04,200 --> 00:06:06,400 [no audio] 94 00:06:06,400 --> 00:06:09,000 Yeah, if it is greater than, right, as per your requirement. 95 00:06:10,400 --> 00:06:12,500 If 'age' is greater than 30, no problem. 96 00:06:14,200 --> 00:06:17,400 Right. Let me take my age as something like 20, 97 00:06:17,400 --> 00:06:19,400 [no audio] 98 00:06:19,400 --> 00:06:24,500 now if 'age' is not greater than 30, right, then 99 00:06:24,500 --> 00:06:26,400 I want to create an 'AssertionError'. 100 00:06:27,300 --> 00:06:32,000 Yes, exception is occurring means 'AssertionError' is running, 101 00:06:32,800 --> 00:06:37,800 raising. So if you want to handle your raised 'AssertionError', 102 00:06:37,800 --> 00:06:42,000 yes, you can handle it like, 'except: if error is 'AssertionError' 103 00:06:42,000 --> 00:06:54,300 then 'print("Raised with assert because age is lessthan 30")'. 104 00:06:54,400 --> 00:06:55,900 That's it. Now see the result. 105 00:06:55,900 --> 00:06:57,700 [no audio] 106 00:06:57,700 --> 00:07:01,500 So 'assert' is going to give an exception called 107 00:07:01,600 --> 00:07:03,500 'AssertionError'. That's it. 108 00:07:04,700 --> 00:07:10,100 Right. So guys for time being just have a look about this 109 00:07:10,400 --> 00:07:13,000 'assert' and 'raise', right. 110 00:07:13,400 --> 00:07:16,900 So while writing real-time scripts based on requirement, 111 00:07:16,900 --> 00:07:18,300 I will use 'raise' sometime, 112 00:07:18,300 --> 00:07:21,400 I will use 'assert' sometime, and simply I will use 'try:' and 113 00:07:21,400 --> 00:07:23,800 'except:'. It's based on situation. 114 00:07:24,300 --> 00:07:27,900 So up to that you have to remember the syntaxes, right and 115 00:07:27,900 --> 00:07:31,000 then we will use there while writing our scripts. 116 00:07:32,500 --> 00:07:34,600 Okay guys, thank you for watching this video. 117 00:07:34,600 --> 00:07:41,429 [no audio]