All language subtitles for 5. [Dart] Exception Handling & Null Aware Operators

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,450 --> 00:00:07,920 So in the last lesson, we managed to get our geolocator to work and grab the current position of the 1 2 00:00:07,920 --> 00:00:11,580 user's device to a low accuracy level. 2 3 00:00:11,580 --> 00:00:15,340 So something like two or three blocks kind of range. 3 4 00:00:15,420 --> 00:00:20,370 Now at the moment all that we're doing is using that one method, 4 5 00:00:20,370 --> 00:00:24,960 but we have to realize that there's a lot of cases where this might fail right? 5 6 00:00:24,990 --> 00:00:30,570 So for example if the user didn't give us permission to get their current position, when that pop up 6 7 00:00:30,570 --> 00:00:34,680 came up to ask them for location permission they said no. 7 8 00:00:34,890 --> 00:00:40,920 There's also other problems such as if the use'rs in a tunnel and it's physically impossible to actually 8 9 00:00:40,920 --> 00:00:44,960 get any signal from them to get their GPS address. 9 10 00:00:45,090 --> 00:00:50,820 So in order to be able to handle all of these errors that might arise we have to learn about how Dart 10 11 00:00:50,910 --> 00:00:53,700 handles exceptions. 11 12 00:00:53,730 --> 00:01:01,440 Now we know that when you write bad code, then our code usually won't even compile. Before it gets to 12 13 00:01:01,440 --> 00:01:08,400 run the errors get caught, they get underlined and it'll tell you there's a problem with your code don't 13 14 00:01:08,400 --> 00:01:11,600 run it and it'll refuse to run the app. 14 15 00:01:11,610 --> 00:01:19,040 So for example let's say that we created a string called myString and we set it equaled this string, 15 16 00:01:19,050 --> 00:01:22,410 so inside single quotes the number 15. 16 17 00:01:22,470 --> 00:01:30,570 Now at this stage if I tried to add the number five to my string, so I'll write myString plus five. 17 18 00:01:30,570 --> 00:01:34,530 Let's try and do some basic arithmetic with strings and numbers. 18 19 00:01:34,530 --> 00:01:41,310 Well it tells me it can't do that because this is an integer and this is a string. 19 20 00:01:41,400 --> 00:01:45,070 It doesn't know how to combine different types. 20 21 00:01:45,180 --> 00:01:48,190 This is what we would call a compile time error. 21 22 00:01:48,360 --> 00:01:54,510 These errors are really easy for us developers to catch because our app won't even run and we'll definitely 22 23 00:01:54,510 --> 00:01:56,100 be alerted to them. 23 24 00:01:56,100 --> 00:02:04,790 When something unexpected happens while your code is running well, then what happens? For example 24 25 00:02:05,120 --> 00:02:13,910 in Dart there is a method that is called parse and it allows us to take a string and turn it into a double. 25 26 00:02:13,910 --> 00:02:18,540 So for example we can say double.parse and we can provide a string. 26 27 00:02:18,560 --> 00:02:21,450 So I'm going to provide myString as a double. 27 28 00:02:21,580 --> 00:02:26,030 And then this method will turn that string into a double. 28 29 00:02:26,030 --> 00:02:30,370 So 15 as a string becomes 15 as a number. 29 30 00:02:30,380 --> 00:02:39,520 Now let's store that inside a double type variable and we'll call it myStringAsADouble. 30 31 00:02:39,540 --> 00:02:42,520 We'll set it to equal the output of that method. 31 32 00:02:42,530 --> 00:02:49,100 Now we don't have any errors anymore because the job of this method is turn strings into doubles. 32 33 00:02:49,100 --> 00:02:54,230 And in this case, it's pretty easy. 15 as a string if you remove the single quotes, it becomes a double 33 34 00:02:54,230 --> 00:02:54,470 right? 34 35 00:02:54,800 --> 00:03:00,470 So we can even print this out and we can say myStringAsADouble 35 36 00:03:00,470 --> 00:03:03,660 and we can even add five to it if we wanted to. 36 37 00:03:03,680 --> 00:03:09,640 Now at this stage there's no compile time errors because all the data types are what they should be. 37 38 00:03:09,740 --> 00:03:16,310 And if I hit run, you can see that my string 15 gets turned into a double 15 and then it gets five added 38 39 00:03:16,310 --> 00:03:16,880 to it. 39 40 00:03:16,880 --> 00:03:19,460 And that gets printed in the console. 40 41 00:03:19,460 --> 00:03:20,960 So that's pretty good. 41 42 00:03:21,080 --> 00:03:27,870 But what if I wrote some bad code and I gave it something that it can't actually work on? 42 43 00:03:27,890 --> 00:03:37,670 So if I turn myString into 'abc' and I try to get my code to pass that string abc into a double and try 43 44 00:03:37,670 --> 00:03:39,140 to add five to it. 44 45 00:03:39,140 --> 00:03:40,660 Well what happens in this case? 45 46 00:03:40,850 --> 00:03:46,610 Now notice how we're not getting any compile time errors because all that it checks at compile time 46 47 00:03:46,700 --> 00:03:52,610 is that for the inputs that you're putting in, it matches the data type that it expects, 47 48 00:03:52,610 --> 00:03:58,040 so in this case parse requires a string data type as the source, 48 49 00:03:58,100 --> 00:04:01,040 and that's exactly what we're providing. We're providing a string. 49 50 00:04:01,070 --> 00:04:05,870 Now it doesn't know that we're actually tricking it and trying to give it an actual piece of text to 50 51 00:04:05,870 --> 00:04:12,170 try and turn into a number, a job that a human can't even do. If you give me the words abc and told me, 51 52 00:04:12,440 --> 00:04:17,950 'give that back to me as a number', I wouldn't really know what to do either let alone our computer. 52 53 00:04:18,410 --> 00:04:24,280 But notice how this error is going to happen in the future while our app is running. 53 54 00:04:24,290 --> 00:04:25,890 So this is fine. 54 55 00:04:26,090 --> 00:04:27,870 And this is fine. 55 56 00:04:27,950 --> 00:04:34,070 But at the point when it tries to turn a piece of text, abc, into a number, that's where it's going to 56 57 00:04:34,070 --> 00:04:35,270 trip over and fail. 57 58 00:04:35,600 --> 00:04:39,800 So that's going to happen while the app is running or during runtime. 58 59 00:04:39,800 --> 00:04:48,200 So now if I hit run and start off our runtime, you can see I get an exception that's not been caught. 59 60 00:04:48,200 --> 00:04:54,110 So an uncaught exception and it tells me that abc is not actually a double. 60 61 00:04:54,110 --> 00:04:58,450 You can't turn this into a double or into a number. 61 62 00:04:58,580 --> 00:05:05,510 So this method here parse, it's really relying on you being truthful and being good and always giving it 62 63 00:05:05,930 --> 00:05:08,210 a string that can be turned into a number. 63 64 00:05:08,600 --> 00:05:17,450 But as our code grows and as it gets larger, we don't always know what's being passed around. And we might 64 65 00:05:17,450 --> 00:05:20,630 make an honest mistake just like this. 65 66 00:05:20,630 --> 00:05:29,970 So we don't want our app to crash. But instead we want to make sure that we catch our exception. So in 66 67 00:05:29,970 --> 00:05:37,420 answer to that question, when something unexpected happens while your code is running, what should you do? 67 68 00:05:37,440 --> 00:05:45,240 Well in that moment the program is going to throw out an error and it's your job as the programmer to 68 69 00:05:45,300 --> 00:05:51,790 catch that error and handle it. And to do that we can use try and catch. 69 70 00:05:51,840 --> 00:05:56,000 So we're going to take the parts that we know can fail, 70 71 00:05:56,010 --> 00:06:04,020 so for example this part, and we're going to wrap it inside a try block. So we're going to say, 'try to 71 72 00:06:04,020 --> 00:06:13,840 do this'. But if it fails, then we're going to catch the error inside a catch block. 72 73 00:06:14,270 --> 00:06:19,790 So we're going to provide catch and then we're going to give the exception a name, so you call it exception 73 74 00:06:19,910 --> 00:06:26,870 exception, or in most cases you're going to see this written as e short for exception or error. 74 75 00:06:26,870 --> 00:06:34,350 Now inside this catch block we can actually print out the error. And now when I run my code, you'll see 75 76 00:06:34,350 --> 00:06:41,580 that instead of having a uncaught exception, my exception is now caught and it's now printed into the 76 77 00:06:41,580 --> 00:06:42,820 console. 77 78 00:06:43,080 --> 00:06:46,710 At this point some of you might ask, well what's the difference? 78 79 00:06:46,710 --> 00:06:48,300 Same thing happened right? 79 80 00:06:48,330 --> 00:06:53,060 You had an exception and we know what the exception is when we look in the console, 80 81 00:06:53,100 --> 00:06:57,760 what's the point of adding in that try and catch block? Well, 81 82 00:06:57,770 --> 00:07:04,760 the difference is that if an exception occurs and it's not caught inside a catch block, your code will 82 83 00:07:04,760 --> 00:07:05,530 crash. 83 84 00:07:05,540 --> 00:07:11,150 That means your app will crash and your users will see a black screen or your app will exit. 84 85 00:07:11,150 --> 00:07:16,460 But if you do catch it, well you can respond to it and you can plan ahead. 85 86 00:07:16,460 --> 00:07:20,530 So let's see try and catch in action in our Flutter app. 86 87 00:07:20,540 --> 00:07:25,830 Let me quickly show you an example of how unhandled exceptions lead to a crash. 87 88 00:07:25,830 --> 00:07:30,760 Also let me show you how we could recover from an exception and avoid a crash. 88 89 00:07:30,770 --> 00:07:37,190 I'm going to delete everything that's currently inside my build method for the loading screen. And instead 89 90 00:07:37,790 --> 00:07:44,060 I'm going to return something based on a result, and that result is going to be based off a string called 90 91 00:07:44,390 --> 00:07:49,010 myMargin and it's going to be equal to 15, 91 92 00:07:49,040 --> 00:07:57,260 but as a string. And then I'm going to return a scaffold widget which is going to contain a body parameter 92 93 00:07:57,800 --> 00:08:08,090 and for the body of the scaffold, it's going to be a container that has a color of red and it's also 93 94 00:08:08,090 --> 00:08:13,280 going to have a margin property which is of course created using the EdgeInsets. 94 95 00:08:13,280 --> 00:08:15,200 And I'm going to have a margin on all four sides. 95 96 00:08:15,800 --> 00:08:21,370 Now the value that I'm going to use in here is going to be a double because it expects a double. 96 97 00:08:21,470 --> 00:08:29,930 And I'm going to use that parse method to turn my string, myMargin, into a double so that it can be used 97 98 00:08:30,080 --> 00:08:31,820 as the margin for my container. 98 99 00:08:32,630 --> 00:08:38,200 So now at this stage if I go ahead and run my app, let's see what happens. 99 100 00:08:38,240 --> 00:08:40,940 You can see that once the app is up and running, 100 101 00:08:41,030 --> 00:08:48,320 I have this red container and it's been given 15 pixels ofmargin from all four edges. 101 102 00:08:48,380 --> 00:08:50,920 based on this line of code. 102 103 00:08:51,200 --> 00:08:52,990 Now let's break our code. 103 104 00:08:53,060 --> 00:09:00,550 Let's change this margin to an actual piece of text which we know can't be parsed into a double. 104 105 00:09:00,590 --> 00:09:07,190 Now at this stage as soon as I hit save, you'll see that hot reload happens and this line of code gets 105 106 00:09:07,190 --> 00:09:09,780 carried out and my app crashes right? 106 107 00:09:09,800 --> 00:09:15,920 I see nothing onscreen and I see a lot of errors inside my console. 107 108 00:09:15,920 --> 00:09:24,110 Now had I been prepared for this and I knew that some at some stage maybe it was possible that something that 108 109 00:09:24,110 --> 00:09:28,600 couldn't be passed into a double, was put into here as the input. 109 110 00:09:29,030 --> 00:09:32,050 Well then I could've caught that in an exception. 110 111 00:09:32,210 --> 00:09:41,360 So let's move this line of code out and let's create a double which is going to be called myMargin 111 112 00:09:41,750 --> 00:09:44,130 AsADouble. 112 113 00:09:44,220 --> 00:09:48,080 And we're going to set it equal to double.parse(myMargin) 113 114 00:09:48,080 --> 00:09:51,400 Now we know that this is the offending line of code, 114 115 00:09:51,410 --> 00:09:53,790 so this is something that could break. Well 115 116 00:09:53,810 --> 00:10:02,660 so in this case we're going to say try and do this, but if you fail then we're going to do something 116 117 00:10:02,660 --> 00:10:02,990 else. 117 118 00:10:03,050 --> 00:10:11,420 So we're going to catch the exception and we're going to print the exception. Also we're going to 118 119 00:10:11,450 --> 00:10:14,960 provide an alternative widget to render. 119 120 00:10:15,380 --> 00:10:17,880 So let me copy that and paste it in here. 120 121 00:10:17,900 --> 00:10:21,890 It's also going to be a scaffold and it's going to have a container. 121 122 00:10:21,890 --> 00:10:31,610 And in this case, myMargin is simply going to have a default value of 30. So in this case we're saying 122 123 00:10:31,670 --> 00:10:37,100 try and turn this margin into a double. 123 124 00:10:37,100 --> 00:10:45,170 And if you can, then use myMarginasADouble inside this container that you're going to render but 124 125 00:10:45,470 --> 00:10:53,330 if there were any errors with this, then try and catch that inside a catch block and not only print the 125 126 00:10:53,330 --> 00:11:01,620 exception into the console but also provide an alternative, so provide a different container. 126 127 00:11:01,640 --> 00:11:09,860 Now at this stage if I hit save and let's reload our app, you can see that even though this code is 127 128 00:11:09,860 --> 00:11:18,260 still failing and I'm getting my exception printed into my console, 'Invalid double' That's abc. I'm getting 128 129 00:11:18,260 --> 00:11:22,090 an alternative reality being rendered on screen. 129 130 00:11:22,130 --> 00:11:25,410 Now if we look at our code, it's actually doing something really simple. 130 131 00:11:25,460 --> 00:11:31,820 It's simply providing a default value for our margin when parsing myMarginasADouble has an error 131 132 00:11:31,970 --> 00:11:35,650 and myMargin doesn't actually have a value. 132 133 00:11:35,690 --> 00:11:42,270 Now we can make this much simpler using another construct from Dart that's really useful. Instead of creating 133 134 00:11:42,270 --> 00:11:48,090 two alternative scaffolds, let's take this out of all try catch block and keep it really simple. 134 135 00:11:48,110 --> 00:11:55,920 So all it does is it tries to turn our margin into a double and then if there were any errors, it'll 135 136 00:11:55,920 --> 00:11:56,740 catch it. 136 137 00:11:57,790 --> 00:12:01,900 And our build method is simply going to try and return a scaffold 137 138 00:12:01,930 --> 00:12:04,080 using this myMargin as double. 138 139 00:12:04,120 --> 00:12:10,740 So let's move that out of the try block, up here, and it's going to start out with no value. 139 140 00:12:10,750 --> 00:12:17,950 So it's going to start out with null, but it's gonna try and turn myMargin into myMarginasADouble 140 141 00:12:18,190 --> 00:12:21,340 by parsing this value myMargin. 141 142 00:12:21,340 --> 00:12:26,410 Now if there were any exceptions it's going to print the exception but we're still going to return a 142 143 00:12:26,410 --> 00:12:27,970 widget nonetheless. 143 144 00:12:28,000 --> 00:12:35,020 Now in this case it's going to use myMarginAsADouble. But if we run our code right now, margin as 144 145 00:12:35,020 --> 00:12:37,770 a double tries to be turned into a double 145 146 00:12:37,780 --> 00:12:43,060 but because it's a piece of text, this fails and the catch block gets triggered. 146 147 00:12:43,060 --> 00:12:49,120 Now in this case we can either say if there were any errors turning into a double, let's change myMargin 147 148 00:12:49,150 --> 00:12:58,180 AsADouble into a default value, let's say a 30 right? Now in this case, you can see that the margin of 148 149 00:12:58,180 --> 00:13:06,070 30 gets given to our red container and it's using it from here because we know that we definitely can't 149 150 00:13:06,070 --> 00:13:08,430 convert abc into 30 150 151 00:13:08,440 --> 00:13:08,680 right? 151 152 00:13:08,680 --> 00:13:13,830 So it's actually the catch block that's being triggered and we see the exception in the console as well. 152 153 00:13:14,230 --> 00:13:20,320 But there's actually a much easier way of doing this checking to see if a particular variable is equal 153 154 00:13:20,320 --> 00:13:21,290 to null, 154 155 00:13:21,430 --> 00:13:24,630 in that case providing a alternative value. 155 156 00:13:24,840 --> 00:13:29,510 And we can do it right here in line with our margin property. 156 157 00:13:29,590 --> 00:13:36,280 So instead of trying to provide a value here, we can say myMarginAsADouble 157 158 00:13:36,280 --> 00:13:38,740 and then we add two question marks. 158 159 00:13:38,740 --> 00:13:45,570 And what this syntax says is that if this has a value i.e. it's not null, then use it. 159 160 00:13:45,790 --> 00:13:50,040 But if it is null, then use my alternative instead. 160 161 00:13:50,320 --> 00:13:52,770 So use 30 in its place. 161 162 00:13:53,380 --> 00:13:56,670 And this line of code works exactly the same as before. 162 163 00:13:56,710 --> 00:14:03,280 You can see that the margin of 30 is being applied here because myMarginAsADouble at the moment is 163 164 00:14:03,280 --> 00:14:04,520 equal to null. 164 165 00:14:04,540 --> 00:14:09,480 And we're also getting some exceptions thrown which we could handle in our catch block. 165 166 00:14:09,490 --> 00:14:15,670 Now let's say that I change this to an actual number that can be parsed so that this line of code actually 166 167 00:14:15,670 --> 00:14:16,540 succeeds. 167 168 00:14:16,660 --> 00:14:23,020 Then when I run the code then you'll see that the margin changes to 15 instead. It's preferentially using 168 169 00:14:23,020 --> 00:14:26,050 this value but if it actually is equal to null, 169 170 00:14:26,170 --> 00:14:32,350 then this operator makes sure that we have a default value in its place so that we don't end up having 170 171 00:14:32,440 --> 00:14:38,700 a blank screen or problems rendering our margins. So 171 172 00:14:38,740 --> 00:14:46,730 in this lesson, we saw the try catch block where we can enclose a line of code that might fail and might 172 173 00:14:46,730 --> 00:14:54,140 throw an error, inside a try block. And if it does throw the error then we'll catch it inside our catch 173 174 00:14:54,140 --> 00:15:00,440 block. And we can either handle the exception by printing it or by putting it onto the screen to alert 174 175 00:15:00,470 --> 00:15:04,740 the user or we can provide an alternative reality in the catch block. 175 176 00:15:04,820 --> 00:15:11,150 We also saw the null aware operator and in this case what happens is that we have some sort of variable 176 177 00:15:11,480 --> 00:15:16,260 that we know could be null at some point, it might not contain a value. 177 178 00:15:16,310 --> 00:15:22,670 So in that case we add two question marks saying that if in the future when we try to use this variable 178 179 00:15:22,790 --> 00:15:27,050 and it's equal to null, then use this default value instead. 179 180 00:15:27,050 --> 00:15:33,650 But if it does in fact have a value then use that value, so that we're aware that a variable can be null, 180 181 00:15:34,130 --> 00:15:37,520 and if it is, we provide an alternative. 181 182 00:15:37,610 --> 00:15:45,320 So let's apply what we've just learned to our code inside getLocation. So we know that this is the line 182 183 00:15:45,320 --> 00:15:51,530 that probably could fail in certain cases, say if we didn't get permission to get the location or if 183 184 00:15:51,530 --> 00:15:57,140 the user is unreachable and we can't figure out their GPS and even the device doesn't know their GPS 184 185 00:15:57,140 --> 00:15:57,990 address. 185 186 00:15:58,010 --> 00:16:00,320 So let's wrap this line of code. 186 187 00:16:00,350 --> 00:16:04,840 So Android Studio is putting it onto two lines just to make it easier to read. 187 188 00:16:04,850 --> 00:16:10,260 But this is in fact all one line of code and we're going to wrap it inside a try block. 188 189 00:16:10,400 --> 00:16:16,160 So I'm going to add the try keyword, add a set of parentheses and then I'm going to wrap everything that 189 190 00:16:16,160 --> 00:16:23,000 depends on that position inside the try block. And then I'm going to follow that with a catch block and 190 191 00:16:23,060 --> 00:16:32,150 inside a set of parentheses, I'm going to put the exception or simply e for short. And inside the catch 191 192 00:16:32,150 --> 00:16:39,290 block we can print the error or exception if it does occur, or if we're being more sophisticated we can 192 193 00:16:39,290 --> 00:16:45,380 actually try and figure out what the error is and decide on an alternative route based on that. 193 194 00:16:45,380 --> 00:16:50,160 So as Benjamin Franklin says by failing to prepare, you're preparing to fail. 194 195 00:16:50,210 --> 00:16:55,670 So we have to make sure that we always prepare for the worst case scenario when something doesn't work 195 196 00:16:55,700 --> 00:16:58,100 or something might throw an exception 196 197 00:16:58,130 --> 00:17:04,210 and we have to try and catch it so that we can handle it or deal with it or provide an alternative path. 197 198 00:17:05,940 --> 00:17:12,150 Now let's talk about the last piece of the puzzle of the try catch block, namely what actually triggers 198 199 00:17:12,180 --> 00:17:13,440 an exception? 199 200 00:17:13,560 --> 00:17:20,280 Where do these exceptions actually get thrown from? The key to understanding this is the throw keyword. 200 201 00:17:21,290 --> 00:17:23,470 A method can throw an exception 201 202 00:17:23,550 --> 00:17:28,930 and by throwing an exception, we can handle the exception in a try catch block. 202 203 00:17:28,950 --> 00:17:32,640 Let me show you how to create a method that throws an exception. 203 204 00:17:32,640 --> 00:17:41,680 So here I'm going to create a new method called somethingThatExpectsLessThan10. 204 205 00:17:41,730 --> 00:17:42,920 So this is really long. 205 206 00:17:42,960 --> 00:17:47,060 And it needs to take an input which is an integer and we'll call that 206 207 00:17:47,100 --> 00:17:52,580 n. Now inside the body of the method, we have to do something with this integer. 207 208 00:17:52,600 --> 00:18:01,140 And but we should expect that in all circumstances, n should always be less than 10. And if n was greater 208 209 00:18:01,140 --> 00:18:04,400 than 10, then something terrible has happened. 209 210 00:18:04,440 --> 00:18:07,080 And in that case we have to throw an exception. 210 211 00:18:07,080 --> 00:18:11,020 We can't work with something larger than 10. 211 212 00:18:11,040 --> 00:18:17,910 So in this case, we would use the through keyword to throw out an error and our exception is going to 212 213 00:18:17,910 --> 00:18:27,520 read, 'n is greater than 10, n should always be less than 10'. 213 214 00:18:27,680 --> 00:18:29,620 So here's our exception. 214 215 00:18:29,680 --> 00:18:37,930 And now let's say inside our getLocation instead of trying to print the position and get the position 215 216 00:18:37,930 --> 00:18:45,970 from our geolocator, we instead use our method somethingThatExpectsLessThan10 and for n, we're going 216 217 00:18:45,970 --> 00:18:51,420 to put in 12 which clearly is going to violate our rules and it's not going to work. 217 218 00:18:51,430 --> 00:18:55,210 So now if we hit run and we check out the console 218 219 00:18:58,470 --> 00:19:04,560 then you can see that we get, 'n is greater than 10, n should always be less than 10' thrown as the 219 220 00:19:04,560 --> 00:19:05,400 exception. 220 221 00:19:05,400 --> 00:19:12,090 But if we didn't handle the exception if we didn't have this try and catch block and we tried to run 221 222 00:19:12,090 --> 00:19:20,600 this code that throws an exception under certain conditions, then our app will actually crash. So let's 222 223 00:19:20,600 --> 00:19:23,600 get rid of that method here and here. 223 224 00:19:24,760 --> 00:19:33,300 And let's bring our code back with the try catch block. Now going back to our getCurrentPosition method, 224 225 00:19:33,340 --> 00:19:37,600 let me show you where this method actually throws an exception. For this, 225 226 00:19:37,690 --> 00:19:41,480 we'll need to peek inside the geo location package. 226 227 00:19:41,680 --> 00:19:48,010 If we take this method and we hold down COMMAND and we click on it, it takes us to where it was declared 227 228 00:19:48,010 --> 00:19:55,270 or where it was created. And you can see here in this method that's created from the geolocator library, 228 229 00:19:55,300 --> 00:20:00,550 you can see that it checks to see if permission is permission status granted, so the user actually allowed 229 230 00:20:00,550 --> 00:20:02,510 you to access their position, 230 231 00:20:02,680 --> 00:20:10,240 and then we try and get the location. But if they didn't let you have their permission, then it would handle 231 232 00:20:10,300 --> 00:20:12,520 the invalid permissions. 232 233 00:20:12,520 --> 00:20:15,810 And here we see how the exceptions are thrown. 233 234 00:20:15,820 --> 00:20:22,240 There are two cases when an exception is thrown by our geo location package, permission denied and permission 234 235 00:20:22,240 --> 00:20:23,570 disabled. 235 236 00:20:23,590 --> 00:20:27,590 This is the throw keyword which triggers the exception inaction. 236 237 00:20:27,610 --> 00:20:31,870 This is why we wrapped the getCurrentPosition method in a try catch block. 27267

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.