1 00:00:00,390 --> 00:00:04,830 In the previous tutorial we have seen different exceptions. 2 00:00:06,170 --> 00:00:14,950 Raised when executing our programs so when the Python interpreter finds a fault in our program that 3 00:00:14,950 --> 00:00:22,920 means the program does not complete as expected or it generates erroneous results. 4 00:00:22,930 --> 00:00:29,590 The python interpreted calls for that execution of the program and reported the error by throwing an 5 00:00:29,590 --> 00:00:31,840 exception for some scripts. 6 00:00:31,840 --> 00:00:40,720 This behavior is sufficient but it is nice to catch the exception and print a more user friendly version 7 00:00:40,780 --> 00:00:51,350 of the error so we can decide how our programs respond to different types of errors or exceptions. 8 00:00:51,470 --> 00:00:56,300 Say for example we may want to prompt the user for more input. 9 00:00:56,540 --> 00:01:06,770 Display extended data or warning information exception handling mechanism allows us to deal with exceptions 10 00:01:06,770 --> 00:01:11,150 and errors gracefully without halting the program. 11 00:01:11,390 --> 00:01:14,450 And in Python we use the try. 12 00:01:14,820 --> 00:01:21,500 Except statement for exception handling and this is the syntax for the try. 13 00:01:21,530 --> 00:01:26,170 Except statement the Code begins with the word. 14 00:01:26,540 --> 00:01:36,050 The key word try which is a reserved keyword in the key word is followed by colon. 15 00:01:36,320 --> 00:01:42,370 The trade clause contains code that might raise an exception. 16 00:01:42,590 --> 00:01:49,430 So these debtors set of statements here might raise an exception and they are put in the trade clause. 17 00:01:49,430 --> 00:02:00,170 After that we have an except clause that starts with the reserved keyword except followed by the exception 18 00:02:00,170 --> 00:02:03,470 type and a colon. 19 00:02:03,620 --> 00:02:14,090 The exception type like we have discussed before can be the Zero Division error type error value error. 20 00:02:14,520 --> 00:02:26,190 And so on and these exceptions are raised from the dry block and the accept blog contains go to handle 21 00:02:26,190 --> 00:02:33,330 the exception when an exception occurs in the trade block execution of list of the statements in the 22 00:02:33,390 --> 00:02:34,730 try block escape. 23 00:02:34,800 --> 00:02:42,570 And if the exception dressed matches the exception type in the except clause the corresponding handler 24 00:02:42,780 --> 00:02:51,360 is executed or the corresponding code is executed decode in the try block and raise any of the exceptions 25 00:02:51,360 --> 00:03:02,460 like the Zero Division error type error value error and so on and the accept blog may declare what kind 26 00:03:02,460 --> 00:03:05,370 of exceptions they handle. 27 00:03:05,430 --> 00:03:15,400 That is the exception type and in the except blog we are going to write code or write a program that 28 00:03:15,400 --> 00:03:18,280 is going to handle the selected exception. 29 00:03:18,520 --> 00:03:23,510 So the response to the exception is put in the accept blog. 30 00:03:23,560 --> 00:03:30,220 Say for example if the exception raised in the tribe blog doesn't match with the exception type specified 31 00:03:30,220 --> 00:03:40,060 in the accept clause the program hard with the trace back No let us see examples of how the how exceptions 32 00:03:40,090 --> 00:03:49,890 can be handled using the dry except statement so here we have declared two variables A and B and indeed 33 00:03:49,950 --> 00:03:59,180 tribe blog we're going to add these two variables and then print digital and indeed except clause we 34 00:03:59,190 --> 00:04:02,400 are going to catch the type error. 35 00:04:02,410 --> 00:04:10,620 This is the exception type type error and this is of response to the exception type error. 36 00:04:10,840 --> 00:04:18,340 If we're going to say that that the user needs to check the input variables the type of the input variables 37 00:04:18,820 --> 00:04:28,570 when we execute this code since a and b the variables A and B are integers they can be added and then 38 00:04:28,600 --> 00:04:29,860 the result is printed. 39 00:04:30,250 --> 00:04:32,320 So there are no exceptions raised. 40 00:04:32,590 --> 00:04:40,210 So the control does not go to the except clause so the statements here are skipped. 41 00:04:41,430 --> 00:04:50,880 Now say for example this time we have assigned a string to the variable E and an integer to debatable 42 00:04:50,900 --> 00:04:51,470 B. 43 00:04:51,480 --> 00:04:58,410 We are trying to concatenate both of these variables and then this and then display the result. 44 00:04:58,650 --> 00:05:06,000 And we also have the accept clause which is going to catch the type error. 45 00:05:06,010 --> 00:05:14,170 Now when we try to concatenate these two variables since the data type of these two variables are not 46 00:05:14,170 --> 00:05:15,970 the same one is a string. 47 00:05:15,970 --> 00:05:18,370 The other is an integer. 48 00:05:18,370 --> 00:05:24,040 This the try block code is going to raise a type error. 49 00:05:24,340 --> 00:05:31,050 So on and then we have the accept clause which is going to catch this exception. 50 00:05:31,240 --> 00:05:37,020 And this is going to be printed to the user check the type of input variables. 51 00:05:37,060 --> 00:05:44,830 So instead of printing the end the entire error message giving details such as a file number line number 52 00:05:45,070 --> 00:05:50,410 and the exception type detected we are going to print this text. 53 00:05:50,800 --> 00:05:59,880 Another example where we are again trying to concatenate an integer and a string in this example we 54 00:05:59,880 --> 00:06:08,810 have an except clause and this time we are catching the Zero Division error. 55 00:06:09,180 --> 00:06:12,080 So when you execute this program did try. 56 00:06:12,080 --> 00:06:17,910 Block is going to raise an exception which is going to be a type error because we are going. 57 00:06:18,000 --> 00:06:24,810 We are trying to concatenate a string and an integer which is not possible there are different data 58 00:06:24,810 --> 00:06:34,380 types so for that reason type error is raised but we have an accept clause which is handling Zero Division 59 00:06:34,470 --> 00:06:34,890 error. 60 00:06:35,250 --> 00:06:46,210 So your accept clause does not catch this exception and if the exception raised in the tribe blog doesn't 61 00:06:46,210 --> 00:06:54,250 match with the exception type specified in the accept clause the program halves with a trace back. 62 00:06:54,250 --> 00:06:57,670 So let's see this example in the Jupiter notebook. 63 00:06:57,730 --> 00:07:01,750 So this is the example that we had just talked about it. 64 00:07:01,790 --> 00:07:06,680 Execute this piece of code. 65 00:07:06,840 --> 00:07:16,590 So as you can see the program has been terminated in the try block a type error has been raised and 66 00:07:16,590 --> 00:07:24,570 since we are not handling or there is no accept clause that is handling this exception in our program 67 00:07:25,410 --> 00:07:33,690 the program has been terminated and a full trace back has been printed in the output and accept clause 68 00:07:34,320 --> 00:07:45,540 can name multiple exceptions in a tuple for example in this case multiple exceptions are included in 69 00:07:45,540 --> 00:07:54,660 the accept clause separated by commas and enclosed in bed and this is a tri statement can have more 70 00:07:54,660 --> 00:08:03,550 than one except clause so in this example we have an except clause to handle only type errors. 71 00:08:03,550 --> 00:08:11,050 And here is the response to this type of errors and then we have another except clause in order to handle 72 00:08:11,140 --> 00:08:13,880 name errors and then so on. 73 00:08:13,900 --> 00:08:24,390 So each except clause handles different exceptions and at most one except clause will get executed. 74 00:08:24,580 --> 00:08:31,050 Next we can also use the accept statement with no exceptions defined. 75 00:08:31,060 --> 00:08:41,950 For example we have the accept clause here with no exception type defined so this kind of dry except 76 00:08:41,960 --> 00:08:46,660 statement catches all the exceptions that are good. 77 00:08:46,850 --> 00:08:51,120 In other words this is genetic for exceptions. 78 00:08:51,380 --> 00:09:00,640 In this example we have the dry end except glass and we have more than one except glass. 79 00:09:00,650 --> 00:09:09,530 In this example and each except clause handles a specific exception type so the input variables are 80 00:09:09,530 --> 00:09:11,400 not debatable is a string the end. 81 00:09:11,450 --> 00:09:18,080 The other variable is an integer and we are trying to concatenate these two variables and print the 82 00:09:18,080 --> 00:09:18,940 result. 83 00:09:18,990 --> 00:09:21,870 Let's execute this piece of code. 84 00:09:21,980 --> 00:09:33,110 And here we have the response from the accept clause which is handling the type of and we have also 85 00:09:33,110 --> 00:09:40,580 discovered that the last accept clause may Omer the exception names to serve as a wildcard so we can 86 00:09:40,580 --> 00:09:44,540 provide a generic except class which handles any exception. 87 00:09:45,810 --> 00:09:51,020 So the last except clause here is the generic except clause. 88 00:09:51,270 --> 00:09:57,530 So when we do not specify any exception it will catch any exception. 89 00:09:58,110 --> 00:10:07,350 So if we do not specify which exception to catch it will catch any but remembered on at most only one 90 00:10:07,530 --> 00:10:16,370 except clause get executed in many programs simply reporting the error isn't enough. 91 00:10:16,530 --> 00:10:24,680 If an error occurs we may have to undo some of the work that has already been completed. 92 00:10:24,750 --> 00:10:31,560 Say for example the database may need to be rolled back or temporary files may need to be deleted or 93 00:10:32,060 --> 00:10:34,640 the resources need to be released. 94 00:10:34,800 --> 00:10:42,420 And in Python The finally clause is commonly used to define clean up actions which must be performed 95 00:10:42,420 --> 00:10:51,830 under any circumstance so the finding Lee block is a place to put a neat code that must execute whether 96 00:10:51,830 --> 00:10:54,800 the try block raised an exception or not. 97 00:10:57,390 --> 00:11:03,150 So we have added the finally clause in our code. 98 00:11:03,150 --> 00:11:06,380 Now let's execute this piece of code. 99 00:11:07,020 --> 00:11:10,410 So there is a type error that has been erased from our code. 100 00:11:10,680 --> 00:11:18,240 And then the control has gone to the except clause which is handling the type error exception from this 101 00:11:18,570 --> 00:11:19,470 except clause. 102 00:11:19,470 --> 00:11:28,290 We have this statement check data type of variables and after executing the accept clause the control 103 00:11:28,290 --> 00:11:32,970 has gone to the finally clause and this statement is printed. 104 00:11:32,970 --> 00:11:38,010 Say for example no exception has been raised from about code. 105 00:11:38,190 --> 00:11:41,880 Let's change the data type of the variable. 106 00:11:42,030 --> 00:11:49,910 Let's say this is is now assigned devalued trail and then execute the code. 107 00:11:49,950 --> 00:11:51,940 So this time there is no exception. 108 00:11:51,940 --> 00:11:57,150 Raise the the sum of these two variables has been printed in the output. 109 00:11:57,150 --> 00:12:05,010 The control has gone to the finally clause and here the text has been printed and this brings us to 110 00:12:05,010 --> 00:12:08,250 the end of the topic at and exceptions.