1 00:00:00,670 --> 00:00:03,010 By tornadoes and exceptions. 2 00:00:04,220 --> 00:00:11,900 When writing a program and executing it, we often encounter errors and errors caused by not following 3 00:00:11,900 --> 00:00:18,750 the proper structure or the syntax of the language is called the syntax error or the passing it. 4 00:00:19,340 --> 00:00:25,340 So whenever we are writing one code and the code is not properly written in the respective syntax as 5 00:00:25,340 --> 00:00:30,200 provided by Python, then obviously we are getting getting some syntax error. 6 00:00:30,230 --> 00:00:31,910 What is the meaning of the term syntax? 7 00:00:32,060 --> 00:00:33,380 Syntax means grammar. 8 00:00:33,830 --> 00:00:39,890 So the grammar in which the grammar which you are supposed to follow to write the statements in Python 9 00:00:39,890 --> 00:00:44,050 programming, if we don't follow that one, then syntax error meticulous. 10 00:00:44,630 --> 00:00:50,210 So sometimes during runtime there are some other errors may take place and they can be also called as 11 00:00:50,210 --> 00:00:51,200 a semantic errors. 12 00:00:51,590 --> 00:00:53,920 So syntax error, semantic errors are there. 13 00:00:54,050 --> 00:01:00,080 So why do it while writing about programs in any programming languages, these shut up errors may come 14 00:01:00,080 --> 00:01:00,720 into the play. 15 00:01:01,160 --> 00:01:09,530 So example, if we missed this column in the if statement, then the if imports are not proper, etc.. 16 00:01:09,800 --> 00:01:17,660 So example, if we miss Kolon in the if statement, then such error may take place, or if the imports 17 00:01:17,660 --> 00:01:20,250 are not proper, then the errors meticulous. 18 00:01:20,900 --> 00:01:22,210 No, these are the errors. 19 00:01:22,230 --> 00:01:23,720 Now what are the exceptions. 20 00:01:23,990 --> 00:01:28,630 So the exceptions are being raised whenever you are having any conditions. 21 00:01:28,940 --> 00:01:32,740 So not let us discuss our Biton exceptions. 22 00:01:33,620 --> 00:01:40,320 Here we're having some Biton building exhibitions, so just consider this exhibition and cause of error. 23 00:01:40,730 --> 00:01:45,490 So one is the assassinator raised when Assad statement fails. 24 00:01:45,560 --> 00:01:49,240 So we are having a separate video to discuss on this assertion. 25 00:01:49,400 --> 00:01:50,470 Please watch that one. 26 00:01:50,660 --> 00:01:57,040 And if that assertion is causing failure, then this assassinator will be raised next. 27 00:01:57,050 --> 00:01:58,050 What is the attributed? 28 00:01:58,080 --> 00:02:03,180 It raised one when the attitude, assessment or the reference fails now. 29 00:02:03,200 --> 00:02:09,980 Next one is the if that is the end of dilator raised or when the input function needs to end up the 30 00:02:09,980 --> 00:02:13,020 file and it is trying to read some content from the file. 31 00:02:13,220 --> 00:02:18,860 So in that case, obviously the file has got exhausted and so the error will be, it will be is the 32 00:02:18,860 --> 00:02:25,010 exception will be raised and that is known as E or if the next one is a floating point, either raised 33 00:02:25,020 --> 00:02:34,160 when a floating point operation fails, generated exit, so raised when a director's closed method closed 34 00:02:34,160 --> 00:02:35,770 method is called. 35 00:02:35,900 --> 00:02:37,670 So there is a generated exit. 36 00:02:38,000 --> 00:02:42,900 Next one is import error raised when the imported module is not found. 37 00:02:43,100 --> 00:02:47,690 Let us what you are trying to import non-pay model, but the non-pay model is not present. 38 00:02:47,840 --> 00:02:50,670 Then obviously the import error will come into the play. 39 00:02:51,020 --> 00:02:56,090 So next, what is the index it raised when the index of a sequence is out of the range? 40 00:02:56,270 --> 00:03:01,280 Let us suppose that having only five elements in the sequence that you want to be able to extract, 41 00:03:01,400 --> 00:03:04,490 or do you want to pick up the gentleman from the sequence? 42 00:03:04,580 --> 00:03:06,880 So obviously it will produce some index error. 43 00:03:07,310 --> 00:03:14,210 So let us go for one practical demonstration, for the better understanding of this file of this python 44 00:03:14,330 --> 00:03:18,020 errors and exceptions, indecision. 45 00:03:18,020 --> 00:03:24,890 We are discussing Python errors and exceptions under the tree block we have written is equal to one 46 00:03:24,890 --> 00:03:29,870 string here and we are trying to convert the string to the integer Anderson. 47 00:03:29,870 --> 00:03:31,760 Value will be assigned onto the variable beat. 48 00:03:32,390 --> 00:03:38,150 But here to see one exception would be amazing because we cannot convert high to an integer. 49 00:03:38,570 --> 00:03:44,690 So let us execute this code and let us see that what will happen here to see this particular block has 50 00:03:44,690 --> 00:03:46,700 got followed by the exit block. 51 00:03:47,120 --> 00:03:52,760 So this exit block will be initiated whenever we are having some error, whenever we are having some 52 00:03:52,760 --> 00:03:54,440 error within that block. 53 00:03:54,830 --> 00:04:01,370 So what has happened is whenever you are trying to convert this E to the integer, it has produced one 54 00:04:01,370 --> 00:04:05,760 error, and that is why the control has come directly to this exception block. 55 00:04:06,200 --> 00:04:12,500 So here this exception block will be denoted by accept and Colon will be there if I write it one statement. 56 00:04:15,640 --> 00:04:20,150 If I had one step, you can find that this statement is not getting executed. 57 00:04:20,290 --> 00:04:26,150 That means from this particular line where that exception has been, the error has been raised automatically. 58 00:04:26,170 --> 00:04:33,280 The control has come to the except block and living that is part of the crime block and executed and 59 00:04:33,280 --> 00:04:34,100 unreachable. 60 00:04:34,510 --> 00:04:39,910 But if I convert this one as 100, if I like this one is 100, you can find that I'm here. 61 00:04:39,910 --> 00:04:40,960 It is getting printed. 62 00:04:41,140 --> 00:04:44,470 That means that the full trade block has got executed. 63 00:04:44,590 --> 00:04:52,180 But exception block, the exit block has got not executed here because that is no exception to convert 64 00:04:52,180 --> 00:04:55,660 100, which is in the form of string to integer. 65 00:04:55,660 --> 00:04:58,870 And as I need to be so we can print the value of behere. 66 00:05:06,460 --> 00:05:09,760 You can find that to be easily 100 is getting printed. 67 00:05:10,240 --> 00:05:15,010 So in this way, you have shown you that how this Trian except blocks are working here. 68 00:05:15,550 --> 00:05:18,430 If I convert it to higher, once again, you're going to find that. 69 00:05:19,760 --> 00:05:26,690 Except Block is getting control, so next one is this one, so getting specific exception, so here 70 00:05:26,790 --> 00:05:33,620 is equal to five and being single digits digital and we are trying to do a baby so as single digits 71 00:05:33,640 --> 00:05:33,910 here. 72 00:05:34,040 --> 00:05:36,950 So a baby will produce one exception here. 73 00:05:36,980 --> 00:05:41,360 This particular error we have given the name, there is a general division error. 74 00:05:41,660 --> 00:05:44,020 So division by zero is not possible. 75 00:05:44,030 --> 00:05:45,940 That is a message to be printed here. 76 00:05:46,190 --> 00:05:47,590 So let's go for the execution. 77 00:05:47,600 --> 00:05:50,710 You can find that division president is not possible. 78 00:05:51,050 --> 00:05:54,080 So if you want to print the value of C. 79 00:05:58,810 --> 00:06:04,350 You can find that that this particular line has become unreachable, but if I make this one out of five, 80 00:06:04,360 --> 00:06:11,230 500 and if I make this one set, then in that case, obviously the except gyro division error, that 81 00:06:11,230 --> 00:06:16,190 block will not get the control and it'll bring the record value for C as 50 here. 82 00:06:16,690 --> 00:06:19,150 So that is our kaching specific exception. 83 00:06:19,180 --> 00:06:20,820 So again, you are making this one at zero. 84 00:06:21,130 --> 00:06:24,400 You can find that it is sprinting. 85 00:06:24,400 --> 00:06:26,860 That division by zero is not possible. 86 00:06:27,100 --> 00:06:33,420 And that means the exit block is getting the control whenever we are trying to divide it by zero here. 87 00:06:33,430 --> 00:06:34,570 BS digital here. 88 00:06:35,140 --> 00:06:36,790 So now let us go for another one. 89 00:06:36,800 --> 00:06:42,010 So exception can be raised also in a in our call that we can raise some exception. 90 00:06:42,030 --> 00:06:46,960 So let us suppose within the trade block we have raised one exception, one error that is a typo in 91 00:06:46,960 --> 00:06:47,100 it. 92 00:06:47,410 --> 00:06:49,860 And exit block is handling that type. 93 00:06:50,200 --> 00:06:52,700 And this particular message is going to get printed. 94 00:06:53,650 --> 00:07:00,490 So that means we can also raise our error within the trade block and extra block should have that particular 95 00:07:00,820 --> 00:07:05,020 error code, and that's why the respective body will be executed. 96 00:07:05,350 --> 00:07:09,010 So here you see we are having this type error instead of this type in it. 97 00:07:09,010 --> 00:07:14,100 If we keep only this, except then also the code will produce this one address. 98 00:07:14,110 --> 00:07:17,680 You can mention the you can mention except type error. 99 00:07:17,680 --> 00:07:22,090 Then also the controller will be coming to this block and type error exception. 100 00:07:22,090 --> 00:07:23,920 Got that message will get printed. 101 00:07:24,460 --> 00:07:26,380 So now let us go for a try. 102 00:07:26,380 --> 00:07:31,000 And finally, so within the try block with a written print in Try Block. 103 00:07:31,210 --> 00:07:33,830 And here you see that is no exception has been raised. 104 00:07:34,240 --> 00:07:37,330 So what did happen after the execution of the trade block? 105 00:07:37,450 --> 00:07:42,220 Then the final block will be executed, except Block will not be executed because no effort has been 106 00:07:42,220 --> 00:07:42,780 raised yet. 107 00:07:43,180 --> 00:07:44,200 So what will happen? 108 00:07:44,200 --> 00:07:48,090 The trade block will get executed and the final block will get executed. 109 00:07:48,100 --> 00:07:49,590 So let us go for the execution. 110 00:07:50,230 --> 00:07:53,780 So in trade block, in the final block, you can find this. 111 00:07:54,040 --> 00:08:00,400 But if we raise one error, if it is one error using this code that is res type error, then what will 112 00:08:00,400 --> 00:08:00,760 happen? 113 00:08:01,030 --> 00:08:02,440 That trade block will be executed. 114 00:08:02,440 --> 00:08:06,580 That means this particular code will be printed, this particular statement or string will get printed 115 00:08:06,940 --> 00:08:08,980 and then this res typedef. 116 00:08:09,010 --> 00:08:12,580 So content will come to this except and see what happens. 117 00:08:12,910 --> 00:08:20,350 You can find that in block, in next block, in the final block that was finally blocked will be will 118 00:08:20,350 --> 00:08:25,650 be executed in spite of the fact whether any error has occurred within the trade block or not. 119 00:08:25,990 --> 00:08:31,300 So that means finally block is that the block which is supposed to get executed always, irrespective 120 00:08:31,300 --> 00:08:34,840 of the fact whether any error has occurred within the trade block or not? 121 00:08:35,320 --> 00:08:40,240 Let us suppose within the trade block we're opening one day to fight how to open one data file, how 122 00:08:40,240 --> 00:08:42,030 to perform the read and write operations. 123 00:08:42,040 --> 00:08:43,990 We have discussed that one in the previous videos. 124 00:08:44,470 --> 00:08:47,110 So let us suppose we're opening one data file. 125 00:08:47,230 --> 00:08:52,300 The data file will be open within the trade block and we are trying to read or write from the data file. 126 00:08:52,570 --> 00:08:56,200 Let us suppose there is some error has occurred while reading the data file. 127 00:08:56,350 --> 00:09:01,570 Some portion of the data file has fallen on the back sector of the hard disk in the case. 128 00:09:01,570 --> 00:09:02,320 What will happen? 129 00:09:02,320 --> 00:09:04,050 Some either it will take place. 130 00:09:04,360 --> 00:09:05,950 So in that case, what will happen? 131 00:09:06,260 --> 00:09:11,110 The exception will be raised and will come to the next block. 132 00:09:11,350 --> 00:09:14,740 But in the final block we will be keeping all the F close. 133 00:09:14,890 --> 00:09:20,570 That means the closing of the file and that really method will be called within the Holmdahl final block. 134 00:09:20,680 --> 00:09:26,180 So what will happen whenever some error has occurred within the trade block, then the block will get 135 00:09:26,180 --> 00:09:28,870 to control and ultimately the file will be closed. 136 00:09:29,080 --> 00:09:34,330 But while performing the reading or writing operation from the data file, which is there, the code 137 00:09:34,330 --> 00:09:35,910 has been written within the trade block. 138 00:09:36,100 --> 00:09:42,430 If that is noise that has occurred, they not only also finally block will get the control and the file 139 00:09:42,430 --> 00:09:43,840 will be closed accordingly. 140 00:09:44,170 --> 00:09:49,480 So that means those particular codes are statements which are supposed to get executed irrespective 141 00:09:49,480 --> 00:09:52,600 of the fact or that errors are occurring or not. 142 00:09:52,930 --> 00:09:55,180 They must be written under the final block. 143 00:09:55,540 --> 00:09:59,920 So in this way, in this particular demonstration, we're giving you the idea what is the purpose of 144 00:09:59,920 --> 00:10:02,920 trade, block, block and a final block. 145 00:10:03,340 --> 00:10:04,570 Thanks for watching this video.