WEBVTT 1 00:00:01.770 --> 00:00:03.480 I mentioned in the last video 2 00:00:03.480 --> 00:00:05.100 there's just a couple more things we I mentioned in the last video 3 00:00:05.100 --> 00:00:06.900 need to look at with exceptions there's just a couple more things we 4 00:00:06.900 --> 00:00:08.730 I guess I said a couple of things but I need to look at with exceptions 5 00:00:08.730 --> 00:00:11.639 really meant 4 so there's two optional I guess I said a couple of things but I 6 00:00:11.639 --> 00:00:13.590 clauses that we can add to a try really meant 4 so there's two optional 7 00:00:13.590 --> 00:00:16.500 statement else and finally so we'll have clauses that we can add to a try 8 00:00:16.500 --> 00:00:18.990 a look at what they are next statement else and finally so we'll have 9 00:00:18.990 --> 00:00:21.420 now it can be important to consider the a look at what they are next 10 00:00:21.420 --> 00:00:23.760 order that you catch exceptions using now it can be important to consider the 11 00:00:23.760 --> 00:00:25.860 accept and i'm going to explain about order that you catch exceptions using 12 00:00:25.860 --> 00:00:28.260 that as well and the fourth thing is accept and i'm going to explain about 13 00:00:28.260 --> 00:00:30.510 raising our own exceptions which you that as well and the fourth thing is 14 00:00:30.510 --> 00:00:32.219 sometimes want to do in your functions raising our own exceptions which you 15 00:00:32.219 --> 00:00:34.739 that is really no sensible action they sometimes want to do in your functions 16 00:00:34.739 --> 00:00:37.079 can take your functions have to catch that is really no sensible action they 17 00:00:37.079 --> 00:00:38.220 the exception can take your functions have to catch 18 00:00:38.220 --> 00:00:40.050 otherwise your program will crash the exception 19 00:00:40.050 --> 00:00:42.989 as we've seen but if you can't take any otherwise your program will crash 20 00:00:42.989 --> 00:00:45.149 sensible action then sometimes the as we've seen but if you can't take any 21 00:00:45.149 --> 00:00:47.670 best approach is to pass the exception sensible action then sometimes the 22 00:00:47.670 --> 00:00:50.070 on to whatever called your function for best approach is to pass the exception 23 00:00:50.070 --> 00:00:52.170 it to deal with going to start with the on to whatever called your function for 24 00:00:52.170 --> 00:00:54.600 finally Clause because our current it to deal with going to start with the 25 00:00:54.600 --> 00:00:56.489 example program provides quite a cool finally Clause because our current 26 00:00:56.489 --> 00:00:58.829 demonstration of the fact that a example program provides quite a cool 27 00:00:58.829 --> 00:01:01.980 finally Claus will always be executed so demonstration of the fact that a 28 00:01:01.980 --> 00:01:04.170 a finally clause starts surprisingly finally Claus will always be executed so 29 00:01:04.170 --> 00:01:06.450 with the keyword finally followed by the a finally clause starts surprisingly 30 00:01:06.450 --> 00:01:08.069 block of code that you want to execute with the keyword finally followed by the 31 00:01:08.069 --> 00:01:11.039 whether an exception was raised or not block of code that you want to execute 32 00:01:11.039 --> 00:01:14.130 let's go ahead and put the finaly clause whether an exception was raised or not 33 00:01:14.130 --> 00:01:16.740 into effect and do that on line 13 so... let's go ahead and put the finaly clause 34 00:01:16.740 --> 00:01:21.090 finally call on the next line as print into effect and do that on line 13 so... 35 00:01:21.090 --> 00:01:24.479 ... finally call on the next line as print 36 00:01:24.479 --> 00:01:30.479 ...now if you use ... 37 00:01:30.479 --> 00:01:33.389 a finaly Claus it must come after all ...now if you use 38 00:01:33.389 --> 00:01:36.090 your except causes right so let's just a finaly Claus it must come after all 39 00:01:36.090 --> 00:01:39.029 run this and i'm just going to add some your except causes right so let's just 40 00:01:39.029 --> 00:01:43.229 valid input 10 and 5 notice the run this and i'm just going to add some 41 00:01:43.229 --> 00:01:45.929 finally clause pop up there 4 and there's valid input 10 and 5 notice the 42 00:01:45.929 --> 00:01:48.450 the finally executing again if we finally clause pop up there 4 and there's 43 00:01:48.450 --> 00:01:51.569 run this again and this time I do my the finally executing again if we 44 00:01:51.569 --> 00:01:53.639 contribute command trick and again run this again and this time I do my 45 00:01:53.639 --> 00:01:57.389 ctrl D on windows or linux notice that contribute command trick and again 46 00:01:57.389 --> 00:01:59.369 the process still executed but the finally ctrl D on windows or linux notice that 47 00:01:59.369 --> 00:02:03.029 clause was still executed as well so the the process still executed but the finally 48 00:02:03.029 --> 00:02:04.709 finally Claus really is executed clause was still executed as well so the 49 00:02:04.709 --> 00:02:06.989 regardless of whether an exception was finally Claus really is executed 50 00:02:06.989 --> 00:02:09.569 handled or not that's particularly regardless of whether an exception was 51 00:02:09.569 --> 00:02:11.400 impressive considering what the code handled or not that's particularly 52 00:02:11.400 --> 00:02:13.410 does when I terminate the input with impressive considering what the code 53 00:02:13.410 --> 00:02:14.710 control D does when I terminate the input with 54 00:02:14.710 --> 00:02:16.750 so in the end of EOF or end of control D 55 00:02:16.750 --> 00:02:20.140 file error exception block on line 11 we so in the end of EOF or end of 56 00:02:20.140 --> 00:02:22.270 we actually called sys.exit to file error exception block on line 11 we 57 00:02:22.270 --> 00:02:24.160 terminate the program but even though we actually called sys.exit to 58 00:02:24.160 --> 00:02:26.410 the program's been told to terminate on terminate the program but even though 59 00:02:26.410 --> 00:02:28.930 line 12 the finally block on line the program's been told to terminate on 60 00:02:28.930 --> 00:02:32.500 fourteen is still executed first so if line 12 the finally block on line 61 00:02:32.500 --> 00:02:34.030 you think about it that really makes fourteen is still executed first so if 62 00:02:34.030 --> 00:02:36.520 finally an ideal place for performing you think about it that really makes 63 00:02:36.520 --> 00:02:38.890 any tidying up that may be required finally an ideal place for performing 64 00:02:38.890 --> 00:02:41.560 before your code finishes now the sorts any tidying up that may be required 65 00:02:41.560 --> 00:02:43.030 of things that you want to do in a before your code finishes now the sorts 66 00:02:43.030 --> 00:02:44.620 finally block would be things like of things that you want to do in a 67 00:02:44.620 --> 00:02:46.900 closing a database cursor or connection finally block would be things like 68 00:02:46.900 --> 00:02:49.360 or closing any open files if your closing a database cursor or connection 69 00:02:49.360 --> 00:02:51.040 program is open any those types of or closing any open files if your 70 00:02:51.040 --> 00:02:53.080 things and that will actually use a program is open any those types of 71 00:02:53.080 --> 00:02:54.670 finally clause when we get back to our things and that will actually use a 72 00:02:54.670 --> 00:02:56.740 database to make sure we close our finally clause when we get back to our 73 00:02:56.740 --> 00:02:57.520 cursors database to make sure we close our 74 00:02:57.520 --> 00:03:00.730 alright so moving on an else clause is cursors 75 00:03:00.730 --> 00:03:02.680 executed if the code in the alright so moving on an else clause is 76 00:03:02.680 --> 00:03:05.200 try block doesn't raise an exception executed if the code in the 77 00:03:05.200 --> 00:03:07.060 so in that sense it's equivalent to the try block doesn't raise an exception 78 00:03:07.060 --> 00:03:10.600 else clause in a four or a while loop if so in that sense it's equivalent to the 79 00:03:10.600 --> 00:03:12.160 you remember the else else clause in a four or a while loop if 80 00:03:12.160 --> 00:03:14.620 in a while loop will execute if the loop you remember the else 81 00:03:14.620 --> 00:03:17.260 terminates normally so it executed in a while loop will execute if the loop 82 00:03:17.260 --> 00:03:20.110 there's no break or return statement to terminates normally so it executed 83 00:03:20.110 --> 00:03:23.530 execute the loop prematurely so the same there's no break or return statement to 84 00:03:23.530 --> 00:03:25.480 is true of else when used with a try execute the loop prematurely so the same 85 00:03:25.480 --> 00:03:28.600 statement if no exception is handled the is true of else when used with a try 86 00:03:28.600 --> 00:03:30.670 else clause will be executed statement if no exception is handled the 87 00:03:30.670 --> 00:03:33.400 otherwise it won't be now the use of the else clause will be executed 88 00:03:33.400 --> 00:03:35.320 word else in these cases perhaps otherwise it won't be now the use of the 89 00:03:35.320 --> 00:03:38.860 unfortunate it may help to pretend that word else in these cases perhaps 90 00:03:38.860 --> 00:03:40.720 a word like completion has been used unfortunate it may help to pretend that 91 00:03:40.720 --> 00:03:43.690 instead but unfortunately it wasn't so a word like completion has been used 92 00:03:43.690 --> 00:03:45.280 we have to remember the two different instead but unfortunately it wasn't so 93 00:03:45.280 --> 00:03:46.480 uses of else we have to remember the two different 94 00:03:46.480 --> 00:03:48.790 an if statement and identifies a block uses of else 95 00:03:48.790 --> 00:03:51.250 that will be executed if no earlier an if statement and identifies a block 96 00:03:51.250 --> 00:03:53.890 condition is true and in for while and that will be executed if no earlier 97 00:03:53.890 --> 00:03:56.590 try it's executed if the previous condition is true and in for while and 98 00:03:56.590 --> 00:03:59.050 code complete successfully so if you try it's executed if the previous 99 00:03:59.050 --> 00:04:00.340 use the else clause in your try code complete successfully so if you 100 00:04:00.340 --> 00:04:01.210 statements use the else clause in your try 101 00:04:01.210 --> 00:04:03.550 it must also come after all the except statements 102 00:04:03.550 --> 00:04:07.120 clauses but before any finally clause if it must also come after all the except 103 00:04:07.120 --> 00:04:07.870 you've got one clauses but before any finally clause if 104 00:04:07.870 --> 00:04:10.480 alright so let's add an else clause to our you've got one 105 00:04:10.480 --> 00:04:12.190 program to see how it works alright so let's add an else clause to our 106 00:04:12.190 --> 00:04:13.600 we're going to print a message if the program to see how it works 107 00:04:13.600 --> 00:04:16.060 division was successful going to come we're going to print a message if the 108 00:04:16.060 --> 00:04:19.750 down here after the code on line 23 division was successful going to come 109 00:04:19.750 --> 00:04:22.840 we're going to add an else on line 24 down here after the code on line 23 110 00:04:22.840 --> 00:04:26.220 ... we're going to add an else on line 24 111 00:04:26.220 --> 00:04:34.380 ....okay ... 112 00:04:34.380 --> 00:04:37.830 so I run this again this time lets enter a ....okay 113 00:04:37.830 --> 00:04:44.130 couple of values so...we so I run this again this time lets enter a 114 00:04:44.130 --> 00:04:47.430 get 90 / 45 is 2.0 and division couple of values so...we 115 00:04:47.430 --> 00:04:50.370 performed successfully so if we run it get 90 / 45 is 2.0 and division 116 00:04:50.370 --> 00:04:52.890 again performed successfully so if we run it 117 00:04:52.890 --> 00:04:54.810 90 this time but 0 for the second again 118 00:04:54.810 --> 00:04:57.420 number you can't divide by 0 90 this time but 0 for the second 119 00:04:57.420 --> 00:04:59.400 and this time we haven't got the message number you can't divide by 0 120 00:04:59.400 --> 00:05:01.680 division perform successfully on screen and this time we haven't got the message 121 00:05:01.680 --> 00:05:05.310 so how this is different to finally well division perform successfully on screen 122 00:05:05.310 --> 00:05:07.620 the code in the else clause only execute so how this is different to finally well 123 00:05:07.620 --> 00:05:10.440 if the triblock completed without raising the code in the else clause only execute 124 00:05:10.440 --> 00:05:11.700 an exception if the triblock completed without raising 125 00:05:11.700 --> 00:05:13.440 so when we attempted to divide by zero an exception 126 00:05:13.440 --> 00:05:16.200 else code didn't execute if i change so when we attempted to divide by zero 127 00:05:16.200 --> 00:05:21.120 else on line 24 now to finally and run else code didn't execute if i change 128 00:05:21.120 --> 00:05:24.810 this again and repeat both tests so 90 else on line 24 now to finally and run 129 00:05:24.810 --> 00:05:29.550 and 45 we correctly get the answer 2.0 this again and repeat both tests so 90 130 00:05:29.550 --> 00:05:31.950 and division perform successfully run it and 45 we correctly get the answer 2.0 131 00:05:31.950 --> 00:05:34.860 again this time 90 and 0 as a and division perform successfully run it 132 00:05:34.860 --> 00:05:37.290 second number and we get the message again this time 90 and 0 as a 133 00:05:37.290 --> 00:05:38.760 showing this time division performed second number and we get the message 134 00:05:38.760 --> 00:05:41.610 successfully so finally is always executed showing this time division performed 135 00:05:41.610 --> 00:05:44.190 even if there's an exception or as you saw successfully so finally is always executed 136 00:05:44.190 --> 00:05:45.630 earlier even if the program is even if there's an exception or as you saw 137 00:05:45.630 --> 00:05:48.930 terminated in the try block so code in earlier even if the program is 138 00:05:48.930 --> 00:05:51.000 the else clause only executed if the try terminated in the try block so code in 139 00:05:51.000 --> 00:05:53.430 completes it won't be executed if the else clause only executed if the try 140 00:05:53.430 --> 00:05:55.200 there's an exception or if the trial completes it won't be executed if 141 00:05:55.200 --> 00:05:58.380 code returns or quits the program now there's an exception or if the trial 142 00:05:58.380 --> 00:05:59.820 you might be wondering this point why we code returns or quits the program now 143 00:05:59.820 --> 00:06:02.340 didn't just put the division perform you might be wondering this point why we 144 00:06:02.340 --> 00:06:04.770 successfully message after the code on didn't just put the division perform 145 00:06:04.770 --> 00:06:07.290 line 20 in other words after we printed successfully message after the code on 146 00:06:07.290 --> 00:06:09.840 out the result in this case we could line 20 in other words after we printed 147 00:06:09.840 --> 00:06:11.010 have done just that out the result in this case we could 148 00:06:11.010 --> 00:06:12.810 that's because our print statement can't have done just that 149 00:06:12.810 --> 00:06:14.370 raise an exception so there's little that's because our print statement can't 150 00:06:14.370 --> 00:06:16.860 point using else clause here else is raise an exception so there's little 151 00:06:16.860 --> 00:06:19.020 really intended so that we don't point using else clause here else is 152 00:06:19.020 --> 00:06:21.300 accidentally catch an exception that really intended so that we don't 153 00:06:21.300 --> 00:06:23.370 wasn't raised by the code we're trying accidentally catch an exception that 154 00:06:23.370 --> 00:06:26.190 to protect with the try block so going wasn't raised by the code we're trying 155 00:06:26.190 --> 00:06:28.380 back to our bank account example if you to protect with the try block so going 156 00:06:28.380 --> 00:06:29.940 were protecting the updating of two back to our bank account example if you 157 00:06:29.940 --> 00:06:32.910 transactions using a triblock would only were protecting the updating of two 158 00:06:32.910 --> 00:06:35.099 want the code to roll back if there was transactions using a triblock would only 159 00:06:35.099 --> 00:06:37.289 an error saving the values to the two want the code to roll back if there was 160 00:06:37.289 --> 00:06:39.490 accounts if we add an error saving the values to the two 161 00:06:39.490 --> 00:06:41.080 a load more code inside the try block accounts if we add 162 00:06:41.080 --> 00:06:43.780 and that code raises an exception then a load more code inside the try block 163 00:06:43.780 --> 00:06:45.069 the transactions will be rolled back and that code raises an exception then 164 00:06:45.069 --> 00:06:48.400 even if they completed successfully and the transactions will be rolled back 165 00:06:48.400 --> 00:06:50.020 will see that in a video coming up soon even if they completed successfully and 166 00:06:50.020 --> 00:06:52.360 where it makes more sense to use an else will see that in a video coming up soon 167 00:06:52.360 --> 00:06:55.000 clause so this last example is a bit where it makes more sense to use an else 168 00:06:55.000 --> 00:06:57.009 contrived to demonstrate really how else clause so this last example is a bit 169 00:06:57.009 --> 00:06:58.870 and finally work and the difference contrived to demonstrate really how else 170 00:06:58.870 --> 00:06:59.800 between them and finally work and the difference 171 00:06:59.800 --> 00:07:01.030 now there's one last thing that this between them 172 00:07:01.030 --> 00:07:03.069 program is useful to demonstrate before now there's one last thing that this 173 00:07:03.069 --> 00:07:04.900 we abandoned it and move on to something program is useful to demonstrate before 174 00:07:04.900 --> 00:07:06.099 else we abandoned it and move on to something 175 00:07:06.099 --> 00:07:07.780 now exceptions like everything else in else 176 00:07:07.780 --> 00:07:11.139 python are objects in python three now exceptions like everything else in 177 00:07:11.139 --> 00:07:12.819 they must be derived from the base python are objects in python three 178 00:07:12.819 --> 00:07:15.310 exception class or one of its subclasses they must be derived from the base 179 00:07:15.310 --> 00:07:18.280 that wasn't the case in python two exception class or one of its subclasses 180 00:07:18.280 --> 00:07:20.710 although it was recommended and that's that wasn't the case in python two 181 00:07:20.710 --> 00:07:21.849 also mentioned at the start of the although it was recommended and that's 182 00:07:21.849 --> 00:07:23.860 document we're looking at the browser also mentioned at the start of the 183 00:07:23.860 --> 00:07:27.400 earlier you can see here in Python document we're looking at the browser 184 00:07:27.400 --> 00:07:28.870 all exceptions must be instance of a class earlier you can see here in Python 185 00:07:28.870 --> 00:07:31.840 instances of a class that derives from all exceptions must be instance of a class 186 00:07:31.840 --> 00:07:34.900 base exception so most exceptions will instances of a class that derives from 187 00:07:34.900 --> 00:07:37.509 be subclasses of exception rather than base exception so most exceptions will 188 00:07:37.509 --> 00:07:39.520 base exception which is the be subclasses of exception rather than 189 00:07:39.520 --> 00:07:41.050 recommendation in the fourth paragraph base exception which is the 190 00:07:41.050 --> 00:07:43.630 on that page programmers are encouraged to recommendation in the fourth paragraph 191 00:07:43.630 --> 00:07:45.669 derive new exceptions from the exception on that page programmers are encouraged to 192 00:07:45.669 --> 00:07:47.590 class or one of its subclasses and not derive new exceptions from the exception 193 00:07:47.590 --> 00:07:49.930 from base exception so why is that class or one of its subclasses and not 194 00:07:49.930 --> 00:07:52.389 important well the first reason is that from base exception so why is that 195 00:07:52.389 --> 00:07:54.759 you can catch exception in your except important well the first reason is that 196 00:07:54.759 --> 00:07:56.349 Clause to catch any exception that's you can catch exception in your except 197 00:07:56.349 --> 00:07:58.389 derived from it so let's actually have a Clause to catch any exception that's 198 00:07:58.389 --> 00:07:59.740 look at how that works in our code derived from it so let's actually have a 199 00:07:59.740 --> 00:08:05.050 so go back to the IDE so on line look at how that works in our code 200 00:08:05.050 --> 00:08:07.810 nine we've got this line except so go back to the IDE so on line 201 00:08:07.810 --> 00:08:11.710 value error now we can nine we've got this line except 202 00:08:11.710 --> 00:08:13.449 check that value area is a subclass of value error now we can 203 00:08:13.449 --> 00:08:15.430 exception by holding down the control check that value area is a subclass of 204 00:08:15.430 --> 00:08:17.800 key or on in my case on a Mac the exception by holding down the control 205 00:08:17.800 --> 00:08:19.810 command key clicking on it to see the key or on in my case on a Mac the 206 00:08:19.810 --> 00:08:23.139 code you can see their class value error command key clicking on it to see the 207 00:08:23.139 --> 00:08:24.909 exception then click on that and have a code you can see their class value error 208 00:08:24.909 --> 00:08:25.630 look exception then click on that and have a 209 00:08:25.630 --> 00:08:28.090 so you can see here that value error look 210 00:08:28.090 --> 00:08:30.940 inherits from exception and if we click so you can see here that value error 211 00:08:30.940 --> 00:08:34.659 on exception inherits from exception and if we click 212 00:08:34.659 --> 00:08:36.130 you can see that exception inherits from on exception 213 00:08:36.130 --> 00:08:38.890 base exception so what that means is we you can see that exception inherits from 214 00:08:38.890 --> 00:08:40.029 can come back to our code gonna base exception so what that means is we 215 00:08:40.029 --> 00:08:43.360 close it down means that we can actually can come back to our code gonna 216 00:08:43.360 --> 00:08:46.029 change this on line 9 and replace that close it down means that we can actually 217 00:08:46.029 --> 00:08:49.720 with exception and if we run this we change this on line 9 and replace that 218 00:08:49.720 --> 00:08:51.560 should find that still copes with exception and if we run this we 219 00:08:51.560 --> 00:08:55.670 with invalid input 90 and 0 should find that still copes 220 00:08:55.670 --> 00:08:58.790 just bogus number valid number and then 0 with invalid input 90 and 0 221 00:08:58.790 --> 00:09:02.960 so still coping and basically just bogus number valid number and then 0 222 00:09:02.960 --> 00:09:04.970 catching those exceptions so still coping and basically 223 00:09:04.970 --> 00:09:07.160 now with that said I'm not recommending that catching those exceptions 224 00:09:07.160 --> 00:09:08.810 you do this or suggesting you do that now with that said I'm not recommending that 225 00:09:08.810 --> 00:09:10.490 it's generally a good idea to be you do this or suggesting you do that 226 00:09:10.490 --> 00:09:12.380 specific in the exceptions you catch it's generally a good idea to be 227 00:09:12.380 --> 00:09:15.410 rather than just catching everything one specific in the exceptions you catch 228 00:09:15.410 --> 00:09:17.029 reason for being specific is that you rather than just catching everything one 229 00:09:17.029 --> 00:09:18.560 can handle the different exceptions reason for being specific is that you 230 00:09:18.560 --> 00:09:20.510 differently now if you can just catch can handle the different exceptions 231 00:09:20.510 --> 00:09:22.010 all exceptions as we're doing here differently now if you can just catch 232 00:09:22.010 --> 00:09:24.170 the code will react in the same way all exceptions as we're doing here 233 00:09:24.170 --> 00:09:26.870 regardless of what exception was raised the code will react in the same way 234 00:09:26.870 --> 00:09:30.230 that may often not be the appropriate regardless of what exception was raised 235 00:09:30.230 --> 00:09:32.540 way to react and in fact we've got a that may often not be the appropriate 236 00:09:32.540 --> 00:09:34.670 problem with our code now so it looks way to react and in fact we've got a 237 00:09:34.670 --> 00:09:36.800 like it worked fine until you understand problem with our code now so it looks 238 00:09:36.800 --> 00:09:38.089 this next bit you probably won't see like it worked fine until you understand 239 00:09:38.089 --> 00:09:41.060 anything wrong by looking at the code now this next bit you probably won't see 240 00:09:41.060 --> 00:09:43.130 IntelliJ is aware that anything wrong by looking at the code now 241 00:09:43.130 --> 00:09:45.020 something is not quite right IntelliJ is aware that 242 00:09:45.020 --> 00:09:46.970 we actually got a warning on line 11 something is not quite right 243 00:09:46.970 --> 00:09:49.130 that's where we catch the end of file we actually got a warning on line 11 244 00:09:49.130 --> 00:09:51.800 error you can see the exception that's where we catch the end of file 245 00:09:51.800 --> 00:09:54.320 superclass of exception class EOF error error you can see the exception 246 00:09:54.320 --> 00:09:56.480 has already been caught so let's superclass of exception class EOF error 247 00:09:56.480 --> 00:09:57.920 actually see the effect of that before has already been caught so let's 248 00:09:57.920 --> 00:09:59.360 talking anymore about it i'm going to actually see the effect of that before 249 00:09:59.360 --> 00:10:02.030 run it again going to do the ctrl D talking anymore about it i'm going to 250 00:10:02.030 --> 00:10:07.010 command D on the mac you can see what's run it again going to do the ctrl D 251 00:10:07.010 --> 00:10:08.450 happening here we're going into a loop command D on the mac you can see what's 252 00:10:08.450 --> 00:10:10.820 our programs running wild again i'm happening here we're going into a loop 253 00:10:10.820 --> 00:10:13.970 going to stop it and cancel out of that our programs running wild again i'm 254 00:10:13.970 --> 00:10:16.730 the question at this point is what went going to stop it and cancel out of that 255 00:10:16.730 --> 00:10:17.420 wrong the question at this point is what went 256 00:10:17.420 --> 00:10:19.820 remember when I said that the order that wrong 257 00:10:19.820 --> 00:10:22.070 you handle the exceptions is important remember when I said that the order that 258 00:10:22.070 --> 00:10:24.650 what's happening here with our code is you handle the exceptions is important 259 00:10:24.650 --> 00:10:27.680 that exception is a subclass of EOF what's happening here with our code is 260 00:10:27.680 --> 00:10:30.710 error now subclass of a base class is that exception is a subclass of EOF 261 00:10:30.710 --> 00:10:33.140 also an instance of its base class so error now subclass of a base class is 262 00:10:33.140 --> 00:10:35.420 this case EOF error is an also an instance of its base class so 263 00:10:35.420 --> 00:10:37.520 instance of exception and when we catch this case EOF error is an 264 00:10:37.520 --> 00:10:40.580 exception on line 9 we're instance of exception and when we catch 265 00:10:40.580 --> 00:10:42.050 actually catching all exceptions that exception on line 9 we're 266 00:10:42.050 --> 00:10:44.990 inherit from it including the eof error actually catching all exceptions that 267 00:10:44.990 --> 00:10:47.060 exception that has been defined on line inherit from it including the eof error 268 00:10:47.060 --> 00:10:49.370 11 so what that means is that the code exception that has been defined on line 269 00:10:49.370 --> 00:10:52.190 that runs when eof error is raised is the 11 so what that means is that the code 270 00:10:52.190 --> 00:10:55.190 actual code on line 10 so therefore the that runs when eof error is raised is the 271 00:10:55.190 --> 00:10:57.230 program doesn't get to exit it just actual code on line 10 so therefore the 272 00:10:57.230 --> 00:10:59.720 prints a message and loops or allows the program doesn't get to exit it just 273 00:10:59.720 --> 00:11:01.580 loop to go around again and again and prints a message and loops or allows the 274 00:11:01.580 --> 00:11:03.980 again because the input streams closed loop to go around again and again and 275 00:11:03.980 --> 00:11:05.180 and we keep getting these again because the input streams closed 276 00:11:05.180 --> 00:11:08.780 eof errors raised by line 7 IntelliJ is and we keep getting these 277 00:11:08.780 --> 00:11:10.070 aware of this and that's why its giving eof errors raised by line 7 IntelliJ is 278 00:11:10.070 --> 00:11:11.000 us a warning aware of this and that's why its giving 279 00:11:11.000 --> 00:11:13.250 so when you're handling multiple us a warning 280 00:11:13.250 --> 00:11:15.860 exceptions like we are here it is very so when you're handling multiple 281 00:11:15.860 --> 00:11:17.210 important that you catch them in the exceptions like we are here it is very 282 00:11:17.210 --> 00:11:18.110 right order important that you catch them in the 283 00:11:18.110 --> 00:11:20.630 so the more specific exceptions have to right order 284 00:11:20.630 --> 00:11:23.180 appear first so you can fix this program so the more specific exceptions have to 285 00:11:23.180 --> 00:11:26.990 up by moving lines 11 and 12 up until appear first so you can fix this program 286 00:11:26.990 --> 00:11:28.490 before we catch the exception on line 9 up by moving lines 11 and 12 up until 287 00:11:28.490 --> 00:11:30.980 so I'm gonna just copy those cut before we catch the exception on line 9 288 00:11:30.980 --> 00:11:34.340 them out and pasted above the code that so I'm gonna just copy those cut 289 00:11:34.340 --> 00:11:37.190 was on line 9 and if you actually run them out and pasted above the code that 290 00:11:37.190 --> 00:11:41.180 this and enter some bogus text that isn't a was on line 9 and if you actually run 291 00:11:41.180 --> 00:11:44.330 number here we get invalid number and this and enter some bogus text that isn't a 292 00:11:44.330 --> 00:11:46.430 now if I come back here again and do a number here we get invalid number and 293 00:11:46.430 --> 00:11:48.620 command on a Mac ctrl D on windows or now if I come back here again and do a 294 00:11:48.620 --> 00:11:51.500 linux we actually get it to exit command on a Mac ctrl D on windows or 295 00:11:51.500 --> 00:11:53.750 correctly with exit code one so you can see linux we actually get it to exit 296 00:11:53.750 --> 00:11:55.940 the code that we generated for the correctly with exit code one so you can see 297 00:11:55.940 --> 00:11:58.730 exception eof error is now being the code that we generated for the 298 00:11:58.730 --> 00:12:00.980 executed correctly when we do the exception eof error is now being 299 00:12:00.980 --> 00:12:03.980 control d e so it's not often executed correctly when we do the 300 00:12:03.980 --> 00:12:06.230 that you specify exception as the control d e so it's not often 301 00:12:06.230 --> 00:12:08.240 exception to handle your except Clause that you specify exception as the 302 00:12:08.240 --> 00:12:10.130 but this applies whether you're catching exception to handle your except Clause 303 00:12:10.130 --> 00:12:12.170 multiple exceptions and some of the ones but this applies whether you're catching 304 00:12:12.170 --> 00:12:14.870 you want to catch derive from others and multiple exceptions and some of the ones 305 00:12:14.870 --> 00:12:16.490 by the way we are getting this other error here you want to catch derive from others and 306 00:12:16.490 --> 00:12:19.910 about two broad exception clause this by the way we are getting this other error here 307 00:12:19.910 --> 00:12:20.990 warning is that perhaps we should be about two broad exception clause this 308 00:12:20.990 --> 00:12:22.010 more specific warning is that perhaps we should be 309 00:12:22.010 --> 00:12:23.420 alright so the last thing i want to more specific 310 00:12:23.420 --> 00:12:26.180 mention is related to this now remember alright so the last thing i want to 311 00:12:26.180 --> 00:12:28.280 from the documentation that not all mention is related to this now remember 312 00:12:28.280 --> 00:12:30.530 exceptions have to be derived from from the documentation that not all 313 00:12:30.530 --> 00:12:32.900 exception the requirement is that they exceptions have to be derived from 314 00:12:32.900 --> 00:12:35.300 derived from base exception or one of exception the requirement is that they 315 00:12:35.300 --> 00:12:37.820 its subclasses so if you really do want derived from base exception or one of 316 00:12:37.820 --> 00:12:40.490 to catch any exception then there's a its subclasses so if you really do want 317 00:12:40.490 --> 00:12:42.380 way to specify the sort of wild card to catch any exception then there's a 318 00:12:42.380 --> 00:12:45.500 exception meaning everything to do that way to specify the sort of wild card 319 00:12:45.500 --> 00:12:47.990 just leave off the exception type so exception meaning everything to do that 320 00:12:47.990 --> 00:12:49.910 the except Clause is just the keyword just leave off the exception type so 321 00:12:49.910 --> 00:12:53.150 accept and a colon so this example is the except Clause is just the keyword 322 00:12:53.150 --> 00:12:55.670 going to delete the exception on line 11 accept and a colon so this example is 323 00:12:55.670 --> 00:12:59.150 and just leave the colon there before I going to delete the exception on line 11 324 00:12:59.150 --> 00:13:01.340 do this just to be clear I'm going to and just leave the colon there before I 325 00:13:01.340 --> 00:13:03.650 change the finally here on line 24 to an else do this just to be clear I'm going to 326 00:13:03.650 --> 00:13:04.160 again change the finally here on line 24 to an else 327 00:13:04.160 --> 00:13:07.190 so that when I make the code again 328 00:13:07.190 --> 00:13:08.870 available in the resources it's showing so that when I make the code 329 00:13:08.870 --> 00:13:10.340 how it should be done rather available in the resources it's showing 330 00:13:10.340 --> 00:13:12.860 demonstrating how not to do it right the how it should be done rather 331 00:13:12.860 --> 00:13:14.540 other thing I'm going to do here is on demonstrating how not to do it right the 332 00:13:14.540 --> 00:13:17.060 line 11 is just to make it clear here other thing I'm going to do here is on 333 00:13:17.060 --> 00:13:18.180 with a comment line 11 is just to make it clear here 334 00:13:18.180 --> 00:13:23.370 we should really be except... with a comment 335 00:13:23.370 --> 00:13:27.990 ... we should really be except... 336 00:13:27.990 --> 00:13:29.490 alright let's test the program a few ... 337 00:13:29.490 --> 00:13:33.029 times let's try a number or partial alright let's test the program a few 338 00:13:33.029 --> 00:13:34.770 numbers i will test for invalid input times let's try a number or partial 339 00:13:34.770 --> 00:13:39.240 using numbers and letters invalid number numbers i will test for invalid input 340 00:13:39.240 --> 00:13:43.380 will try just letters that works and try using numbers and letters invalid number 341 00:13:43.380 --> 00:13:52.050 76.....that works ok running will try just letters that works and try 342 00:13:52.050 --> 00:13:56.040 again and again we're getting the errors 76.....that works ok running 343 00:13:56.040 --> 00:13:58.350 caught successfully invalid numbers by again and again we're getting the errors 344 00:13:58.350 --> 00:14:00.660 the Wildcard accept on line 11 so that's caught successfully invalid numbers by 345 00:14:00.660 --> 00:14:03.089 good right second time lets do a divided the Wildcard accept on line 11 so that's 346 00:14:03.089 --> 00:14:07.500 by 0 so 32 and 0 the finally clause always good right second time lets do a divided 347 00:14:07.500 --> 00:14:09.899 execute and you can't divide by 0 and by 0 so 32 and 0 the finally clause always 348 00:14:09.899 --> 00:14:11.850 we done a assist.exit 2 as you can see execute and you can't divide by 0 and 349 00:14:11.850 --> 00:14:14.730 there the code on line 23 was executed and we done a assist.exit 2 as you can see 350 00:14:14.730 --> 00:14:17.490 let's do the control d check again run there the code on line 23 was executed and 351 00:14:17.490 --> 00:14:20.820 that and do a control command D on a mac let's do the control d check again run 352 00:14:20.820 --> 00:14:22.560 and control d on a windows that and do a control command D on a mac 353 00:14:22.560 --> 00:14:25.380 or linux and the finally clause and control d on a windows 354 00:14:25.380 --> 00:14:27.390 always execute and we're finishing the or linux and the finally clause 355 00:14:27.390 --> 00:14:30.540 code on line 10 is executed always execute and we're finishing the 356 00:14:30.540 --> 00:14:31.320 correctly code on line 10 is executed 357 00:14:31.320 --> 00:14:33.480 so everything's working so correctly 358 00:14:33.480 --> 00:14:35.279 that's exceptions and how to handle them so everything's working so 359 00:14:35.279 --> 00:14:37.650 in python so in the next video we can that's exceptions and how to handle them 360 00:14:37.650 --> 00:14:38.940 have a quick look at how to raise our in python so in the next video we can 361 00:14:38.940 --> 00:14:41.880 own exceptions before we ultimately get have a quick look at how to raise our 362 00:14:41.880 --> 00:14:43.890 back to databases so see you as own exceptions before we ultimately get 363 00:14:43.890 --> 00:14:50.000 always in the next video back to databases so see you as