WEBVTT 1 00:00:01.820 --> 00:00:03.560 alright so let's have a look in more 2 00:00:03.560 --> 00:00:05.480 detail now the stack trace has been alright so let's have a look in more 3 00:00:05.480 --> 00:00:07.670 printed out here now because of the way detail now the stack trace has been 4 00:00:07.670 --> 00:00:10.070 that the stack trace is generated it may printed out here now because of the way 5 00:00:10.070 --> 00:00:11.870 be interspersed with the program's that the stack trace is generated it may 6 00:00:11.870 --> 00:00:14.329 output if i run the program a few times be interspersed with the program's 7 00:00:14.329 --> 00:00:17.090 you can probably see or you will see we output if i run the program a few times 8 00:00:17.090 --> 00:00:20.210 don't always get the exact same contents you can probably see or you will see we 9 00:00:20.210 --> 00:00:22.009 well technically the content will be don't always get the exact same contents 10 00:00:22.009 --> 00:00:24.050 identical each time but the stack trace well technically the content will be 11 00:00:24.050 --> 00:00:26.630 and the output can get mixed up and when identical each time but the stack trace 12 00:00:26.630 --> 00:00:28.460 it is mixed up we can't rely on what's and the output can get mixed up and when 13 00:00:28.460 --> 00:00:30.980 been printed first we can say every time it is mixed up we can't rely on what's 14 00:00:30.980 --> 00:00:33.170 is that 4 ducks fly before we get an been printed first we can say every time 15 00:00:33.170 --> 00:00:35.270 error to show you what I mean lets run is that 4 ducks fly before we get an 16 00:00:35.270 --> 00:00:37.700 this a few times so you can see that error to show you what I mean lets run 17 00:00:37.700 --> 00:00:39.440 the stack trace is different there this a few times so you can see that 18 00:00:39.440 --> 00:00:41.989 we've got dispersed in a different the stack trace is different there 19 00:00:41.989 --> 00:00:44.210 position than it was previously run it we've got dispersed in a different 20 00:00:44.210 --> 00:00:45.559 again we three at the top position than it was previously run it 21 00:00:45.559 --> 00:00:50.120 three lines of output and again we're again we three at the top 22 00:00:50.120 --> 00:00:51.289 getting different results pretty much three lines of output and again we're 23 00:00:51.289 --> 00:00:54.019 every time we run it so we can't rely on getting different results pretty much 24 00:00:54.019 --> 00:00:55.850 what's been printed first all we can really every time we run it so we can't rely on 25 00:00:55.850 --> 00:00:58.100 say every time is that the four ducks what's been printed first all we can really 26 00:00:58.100 --> 00:01:00.949 fly before we get an error and that's say every time is that the four ducks 27 00:01:00.949 --> 00:01:03.170 true even if the trace starts to print fly before we get an error and that's 28 00:01:03.170 --> 00:01:05.300 before we see the fourth duck take to true even if the trace starts to print 29 00:01:05.300 --> 00:01:07.880 the air so reading from the bottom first before we see the fourth duck take to 30 00:01:07.880 --> 00:01:10.100 and ignoring any text that doesn't the air so reading from the bottom first 31 00:01:10.100 --> 00:01:11.810 appear mostly in red and ignoring any text that doesn't 32 00:01:11.810 --> 00:01:14.390 we've got an attribute error because the appear mostly in red 33 00:01:14.390 --> 00:01:16.460 penguin has no attribute called fly we've got an attribute error because the 34 00:01:16.460 --> 00:01:18.770 right here down the bottom up until that penguin has no attribute called fly 35 00:01:18.770 --> 00:01:22.100 the error as you can see here occurred on right here down the bottom up until that 36 00:01:22.100 --> 00:01:25.610 line 55 the migrate method and that's the error as you can see here occurred on 37 00:01:25.610 --> 00:01:28.369 when it tried to called duck.fly so line 55 the migrate method and that's 38 00:01:28.369 --> 00:01:30.530 that's useful because it tells us where when it tried to called duck.fly so 39 00:01:30.530 --> 00:01:31.729 the error happened that's useful because it tells us where 40 00:01:31.729 --> 00:01:34.070 however we know there's not a problem the error happened 41 00:01:34.070 --> 00:01:35.689 with that particular line of code however we know there's not a problem 42 00:01:35.689 --> 00:01:37.939 because we've seen 4 ducks already with that particular line of code 43 00:01:37.939 --> 00:01:38.990 start flying because we've seen 4 ducks already 44 00:01:38.990 --> 00:01:40.790 so this is where the stack trace is start flying 45 00:01:40.790 --> 00:01:43.460 useful now we've got code that works as so this is where the stack trace is 46 00:01:43.460 --> 00:01:46.040 long as its operating on the correct type useful now we've got code that works as 47 00:01:46.040 --> 00:01:47.960 of object to find out what's really long as its operating on the correct type 48 00:01:47.960 --> 00:01:50.479 caused the error we have to keep reading of object to find out what's really 49 00:01:50.479 --> 00:01:53.630 up the call stack so next we can see the caused the error we have to keep reading 50 00:01:53.630 --> 00:01:56.570 migrate method was called on line 22 up the call stack so next we can see the 51 00:01:56.570 --> 00:01:58.670 duck.migrate as you can see here migrate method was called on line 22 52 00:01:58.670 --> 00:02:03.020 line 22 and in module and that means the duck.migrate as you can see here 53 00:02:03.020 --> 00:02:05.539 main program when it called the flock . line 22 and in module and that means the 54 00:02:05.539 --> 00:02:08.810 migrate method now sometimes that's all main program when it called the flock . 55 00:02:08.810 --> 00:02:09.770 you need migrate method now sometimes that's all 56 00:02:09.770 --> 00:02:12.260 so if migrate accepted arguments that you need 57 00:02:12.260 --> 00:02:13.670 it would probably indicate a problem so if migrate accepted arguments that 58 00:02:13.670 --> 00:02:14.819 with one of the parameters it would probably indicate a problem 59 00:02:14.819 --> 00:02:17.579 but here though it's not quite with one of the parameters 60 00:02:17.579 --> 00:02:20.010 that simple because we set up the flock but here though it's not quite 61 00:02:20.010 --> 00:02:22.859 earlier in the code but there are now that simple because we set up the flock 62 00:02:22.859 --> 00:02:24.180 enough clues to let us know that earlier in the code but there are now 63 00:02:24.180 --> 00:02:27.599 somehow the flocks got a penguin it now enough clues to let us know that 64 00:02:27.599 --> 00:02:29.340 the important thing is that the cause somehow the flocks got a penguin it now 65 00:02:29.340 --> 00:02:31.650 of the error in this case isn't in ducks the important thing is that the cause 66 00:02:31.650 --> 00:02:34.379 .py it's in a migration .py of the error in this case isn't in ducks 67 00:02:34.379 --> 00:02:36.900 module now we've only got two modules here .py it's in a migration .py 68 00:02:36.900 --> 00:02:39.569 but we could have a coordinator program module now we've only got two modules here 69 00:02:39.569 --> 00:02:41.819 that calls another program to select but we could have a coordinator program 70 00:02:41.819 --> 00:02:44.040 different species of ducks that in turn that calls another program to select 71 00:02:44.040 --> 00:02:46.650 calls our migrate module that uses different species of ducks that in turn 72 00:02:46.650 --> 00:02:49.319 the classes from ducks.py and calls our migrate module that uses 73 00:02:49.319 --> 00:02:51.599 things can get quite complex but with a the classes from ducks.py and 74 00:02:51.599 --> 00:02:53.400 stack trace like this it's much easier things can get quite complex but with a 75 00:02:53.400 --> 00:02:56.099 to find the root cause of errors and stack trace like this it's much easier 76 00:02:56.099 --> 00:02:58.829 crashes alright so let's go see what to find the root cause of errors and 77 00:02:58.829 --> 00:03:01.650 we can do about this exception now in crashes alright so let's go see what 78 00:03:01.650 --> 00:03:03.150 fact we've already done the first thing we can do about this exception now in 79 00:03:03.150 --> 00:03:06.060 we could do we have done nothing so we fact we've already done the first thing 80 00:03:06.060 --> 00:03:08.099 didn't handle the exception in the we could do we have done nothing so we 81 00:03:08.099 --> 00:03:10.109 migrate method so the exception didn't handle the exception in the 82 00:03:10.109 --> 00:03:13.230 propagated up the call stack to wherever migrate method so the exception 83 00:03:13.230 --> 00:03:15.719 called migrate and that was our main propagated up the call stack to wherever 84 00:03:15.719 --> 00:03:18.120 program and because that didn't handle called migrate and that was our main 85 00:03:18.120 --> 00:03:19.079 the exception program and because that didn't handle 86 00:03:19.079 --> 00:03:21.299 it got propagated up the python run time the exception 87 00:03:21.299 --> 00:03:24.419 the Python runtime prints out this useful it got propagated up the python run time 88 00:03:24.419 --> 00:03:26.250 stacked trace and then terminates the the Python runtime prints out this useful 89 00:03:26.250 --> 00:03:29.489 program so if an exception isn't handled stacked trace and then terminates the 90 00:03:29.489 --> 00:03:31.439 it will get passed up the call stack program so if an exception isn't handled 91 00:03:31.439 --> 00:03:34.409 until either something handles it or it it will get passed up the call stack 92 00:03:34.409 --> 00:03:37.049 reaches the python runtime now having until either something handles it or it 93 00:03:37.049 --> 00:03:38.909 a program crashed like this isn't good reaches the python runtime now having 94 00:03:38.909 --> 00:03:40.859 but that doesn't mean that migrate a program crashed like this isn't good 95 00:03:40.859 --> 00:03:42.989 method should handle the error it's so but that doesn't mean that migrate 96 00:03:42.989 --> 00:03:44.939 often better to leave errors for method should handle the error it's so 97 00:03:44.939 --> 00:03:46.470 something further up the call chain to often better to leave errors for 98 00:03:46.470 --> 00:03:49.439 deal with so that's one option now is something further up the call chain to 99 00:03:49.439 --> 00:03:51.060 there anything useful that the migrate deal with so that's one option now is 100 00:03:51.060 --> 00:03:53.400 method could do in this case if there anything useful that the migrate 101 00:03:53.400 --> 00:03:55.620 there is we should actually do it but if method could do in this case if 102 00:03:55.620 --> 00:03:57.359 there isn't then we should let the there is we should actually do it but if 103 00:03:57.359 --> 00:03:59.759 exception propagate to allow code there isn't then we should let the 104 00:03:59.759 --> 00:04:01.409 that's in a better position to deal with exception propagate to allow code 105 00:04:01.409 --> 00:04:04.319 the situation handle it now I don't know that's in a better position to deal with 106 00:04:04.319 --> 00:04:05.939 much about ducks and i'm pretty sure the situation handle it now I don't know 107 00:04:05.939 --> 00:04:07.290 that once you've caused the flock of much about ducks and i'm pretty sure 108 00:04:07.290 --> 00:04:09.000 ducks to take flight that once you've caused the flock of 109 00:04:09.000 --> 00:04:10.349 there's no way to get them back on the ducks to take flight 110 00:04:10.349 --> 00:04:13.919 ground now humane way that is but we can there's no way to get them back on the 111 00:04:13.919 --> 00:04:15.359 make sure that all the ducks that are ground now humane way that is but we can 112 00:04:15.359 --> 00:04:18.630 capable of flying managed to migrate so make sure that all the ducks that are 113 00:04:18.630 --> 00:04:19.620 to do that I'm going to start by capable of flying managed to migrate so 114 00:04:19.620 --> 00:04:22.169 catching the exception in the migrate to do that I'm going to start by 115 00:04:22.169 --> 00:04:26.580 method let's have a look at doing that catching the exception in the migrate 116 00:04:26.580 --> 00:04:28.590 i'm looking at our migrate method here method let's have a look at doing that 117 00:04:28.590 --> 00:04:32.939 so after the four we're going to.... i'm looking at our migrate method here 118 00:04:32.939 --> 00:04:38.159 ... so after the four we're going to.... 119 00:04:38.159 --> 00:04:49.110 ... ... 120 00:04:49.110 --> 00:04:53.759 so this time you can see that all seven ... 121 00:04:53.759 --> 00:04:56.340 ducks manage to take off poor Percy is left so this time you can see that all seven 122 00:04:56.340 --> 00:04:58.439 on the ground but at least the rest of ducks manage to take off poor Percy is left 123 00:04:58.439 --> 00:05:02.099 the flock are away but unfortunately the on the ground but at least the rest of 124 00:05:02.099 --> 00:05:04.620 main programs now and none the wiser as the flock are away but unfortunately the 125 00:05:04.620 --> 00:05:06.180 far as our migration program is main programs now and none the wiser as 126 00:05:06.180 --> 00:05:08.430 concerned we've got eight birds in the far as our migration program is 127 00:05:08.430 --> 00:05:10.919 air so we didn't take any action in the concerned we've got eight birds in the 128 00:05:10.919 --> 00:05:13.139 exception handler so the exception was air so we didn't take any action in the 129 00:05:13.139 --> 00:05:15.990 effectively just ignored so what we can exception handler so the exception was 130 00:05:15.990 --> 00:05:21.090 do now that all the ducks safely flying effectively just ignored so what we can 131 00:05:21.090 --> 00:05:23.759 is to raise the error that we've got do now that all the ducks safely flying 132 00:05:23.759 --> 00:05:26.520 when Percy attempted to fly now if we is to raise the error that we've got 133 00:05:26.520 --> 00:05:28.979 attempt to do that within the for loop the when Percy attempted to fly now if we 134 00:05:28.979 --> 00:05:31.259 loop will terminate when we raise an attempt to do that within the for loop the 135 00:05:31.259 --> 00:05:33.810 exception so raising it in the exception loop will terminate when we raise an 136 00:05:33.810 --> 00:05:35.819 handler will leave us no better off than exception so raising it in the exception 137 00:05:35.819 --> 00:05:38.699 before and to show you what i mean i'm handler will leave us no better off than 138 00:05:38.699 --> 00:05:41.490 going to change this Pass will print a before and to show you what i mean i'm 139 00:05:41.490 --> 00:05:47.580 message here one duck down they're and going to change this Pass will print a 140 00:05:47.580 --> 00:05:53.129 put raise run that again we're back to message here one duck down they're and 141 00:05:53.129 --> 00:05:54.659 the same scenario where we were before put raise run that again we're back to 142 00:05:54.659 --> 00:05:56.849 or be that we've got an extra message the same scenario where we were before 143 00:05:56.849 --> 00:05:58.889 here informing us that the percy or be that we've got an extra message 144 00:05:58.889 --> 00:06:01.289 didn't make it but importantly with now here informing us that the percy 145 00:06:01.289 --> 00:06:03.029 unfortunately only got four ducks in didn't make it but importantly with now 146 00:06:03.029 --> 00:06:06.210 flight now you can log the values of unfortunately only got four ducks in 147 00:06:06.210 --> 00:06:08.400 local variables for example before flight now you can log the values of 148 00:06:08.400 --> 00:06:10.800 raising the exception again some of the local variables for example before 149 00:06:10.800 --> 00:06:15.389 words are here and let the caller raising the exception again some of the 150 00:06:15.389 --> 00:06:18.240 deal with it so to raise the exception words are here and let the caller 151 00:06:18.240 --> 00:06:20.610 that you just caught you just used the deal with it so to raise the exception 152 00:06:20.610 --> 00:06:23.250 raise statement that has been typed in and that you just caught you just used the 153 00:06:23.250 --> 00:06:26.099 actually preserves the stack trace so raise statement that has been typed in and 154 00:06:26.099 --> 00:06:27.599 that all the useful information is still actually preserves the stack trace so 155 00:06:27.599 --> 00:06:29.849 available for debugging and again if you that all the useful information is still 156 00:06:29.849 --> 00:06:31.589 run that again you can see that's the available for debugging and again if you 157 00:06:31.589 --> 00:06:34.529 case so we still got our information run that again you can see that's the 158 00:06:34.529 --> 00:06:36.479 and pir stack trace which we can see case so we still got our information 159 00:06:36.479 --> 00:06:38.699 propagating up the call stack much as and pir stack trace which we can see 160 00:06:38.699 --> 00:06:39.900 the case was before propagating up the call stack much as 161 00:06:39.900 --> 00:06:42.930 so basically all the useful information the case was before 162 00:06:42.930 --> 00:06:46.080 still available for debugging npw as O have so basically all the useful information 163 00:06:46.080 --> 00:06:48.180 said many times before everything in still available for debugging npw as O have 164 00:06:48.180 --> 00:06:50.639 python is an object exceptions are no said many times before everything in 165 00:06:50.639 --> 00:06:51.870 exception python is an object exceptions are no 166 00:06:51.870 --> 00:06:53.580 so we can obtain a reference to the exception 167 00:06:53.580 --> 00:06:56.910 exception that's just been raised and we so we can obtain a reference to the 168 00:06:56.910 --> 00:06:58.620 do that with a slightly different form exception that's just been raised and we 169 00:06:58.620 --> 00:07:02.789 of the except Clause sp come back to do that with a slightly different form 170 00:07:02.789 --> 00:07:05.460 our ducks.py to our migrated method of the except Clause sp come back to 171 00:07:05.460 --> 00:07:07.979 and we do instead of typing accept our ducks.py to our migrated method 172 00:07:07.979 --> 00:07:11.100 attribute error at the end of that we do and we do instead of typing accept 173 00:07:11.100 --> 00:07:15.000 ....so when used like this a attribute error at the end of that we do 174 00:07:15.000 --> 00:07:16.800 reference to the exception is stored in ....so when used like this a 175 00:07:16.800 --> 00:07:19.229 a variable for us to do something with reference to the exception is stored in 176 00:07:19.229 --> 00:07:21.840 now one useful thing we could do is a variable for us to do something with 177 00:07:21.840 --> 00:07:24.060 store the exception then raise it once now one useful thing we could do is 178 00:07:24.060 --> 00:07:26.280 all the Ducks are off the ground so to store the exception then raise it once 179 00:07:26.280 --> 00:07:27.660 show you what I mean will delete the all the Ducks are off the ground so to 180 00:07:27.660 --> 00:07:31.710 raise line which always commented out and show you what I mean will delete the 181 00:07:31.710 --> 00:07:37.169 we'll put.. raise line which always commented out and 182 00:07:37.169 --> 00:07:40.050 ... we'll put.. 183 00:07:40.050 --> 00:07:48.240 ... ... 184 00:07:48.240 --> 00:07:49.949 you can see we've got problem here local variable problem ... 185 00:07:49.949 --> 00:07:52.260 might be referenced before assignment so you can see we've got problem here local variable problem 186 00:07:52.260 --> 00:07:55.020 to fix that we should really assign might be referenced before assignment so 187 00:07:55.020 --> 00:07:57.180 value to this outside of the for loop so to fix that we should really assign 188 00:07:57.180 --> 00:07:59.400 problem equals and we'll start with none value to this outside of the for loop so 189 00:07:59.400 --> 00:08:01.620 that then remove that error problem equals and we'll start with none 190 00:08:01.620 --> 00:08:03.810 so let's now run this to see what that then remove that error 191 00:08:03.810 --> 00:08:04.740 happens so let's now run this to see what 192 00:08:04.740 --> 00:08:08.699 you can see this time we've actually got happens 193 00:08:08.699 --> 00:08:11.099 all seven ducks in the air one duck down you can see this time we've actually got 194 00:08:11.099 --> 00:08:13.139 for penguins didn't work and then we've all seven ducks in the air one duck down 195 00:08:13.139 --> 00:08:15.479 got the error at the end so in other for penguins didn't work and then we've 196 00:08:15.479 --> 00:08:17.970 words that the seven ducks that can fly got the error at the end so in other 197 00:08:17.970 --> 00:08:20.370 do so and our migration programs words that the seven ducks that can fly 198 00:08:20.370 --> 00:08:22.349 notified of the problem once they are all do so and our migration programs 199 00:08:22.349 --> 00:08:24.990 of the ground now you probably won't do notified of the problem once they are all 200 00:08:24.990 --> 00:08:27.990 that very often but it can be useful and of the ground now you probably won't do 201 00:08:27.990 --> 00:08:29.940 it also shows another way to use raise that very often but it can be useful and 202 00:08:29.940 --> 00:08:32.580 which is to specify the exception that it also shows another way to use raise 203 00:08:32.580 --> 00:08:35.880 you want to raise so looking at the stack which is to specify the exception that 204 00:08:35.880 --> 00:08:38.010 trace now we still got all the useful you want to raise so looking at the stack 205 00:08:38.010 --> 00:08:40.500 information that we had before the trace now we still got all the useful 206 00:08:40.500 --> 00:08:44.130 original problem was on line 57 ducks.py information that we had before the 207 00:08:44.130 --> 00:08:46.140 i can click that link to have a look original problem was on line 57 ducks.py 208 00:08:46.140 --> 00:08:46.770 at it i can click that link to have a look 209 00:08:46.770 --> 00:08:49.380 that's the duck .fly called their we got at it 210 00:08:49.380 --> 00:08:50.760 another entry this time and clicking that's the duck .fly called their we got 211 00:08:50.760 --> 00:08:51.959 that takes us to the line where the another entry this time and clicking 212 00:08:51.959 --> 00:08:53.430 exception was raised that takes us to the line where the 213 00:08:53.430 --> 00:08:56.520 this one here in line 63 and just move exception was raised 214 00:08:56.520 --> 00:08:59.670 this down a little bit line 63 is we've this one here in line 63 and just move 215 00:08:59.670 --> 00:09:02.430 raised the exception clicking on the this down a little bit line 63 is we've 216 00:09:02.430 --> 00:09:05.279 first link takes us to well the actual raised the exception clicking on the 217 00:09:05.279 --> 00:09:07.830 line where we call the migrate method first link takes us to well the actual 218 00:09:07.830 --> 00:09:11.370 so the stack trace is slightly different line where we call the migrate method 219 00:09:11.370 --> 00:09:13.170 but it still contains all the so the stack trace is slightly different 220 00:09:13.170 --> 00:09:16.620 information you had before so but it still contains all the 221 00:09:16.620 --> 00:09:18.630 let's end the video here in the next information you had before so 222 00:09:18.630 --> 00:09:21.209 video we're going to talk about creating let's end the video here in the next 223 00:09:21.209 --> 00:09:23.130 your own exceptions see you in the next video we're going to talk about creating 224 00:09:23.130 --> 00:09:28.490 video your own exceptions see you in the next