1 00:00:06,120 --> 00:00:11,280 The most thorough way to catch Aires is by using the match expression we learned about earlier on in 2 00:00:11,280 --> 00:00:12,090 this course. 3 00:00:12,480 --> 00:00:17,760 So let's create one so we can say let file equals match file. 4 00:00:18,810 --> 00:00:26,010 And then if an OC is returned with a file, then just continue on with the file. 5 00:00:26,010 --> 00:00:37,860 But if an error is returned, then let's panic air and then let's also pass an error message that it 6 00:00:37,860 --> 00:00:38,670 gives us 7 00:00:41,190 --> 00:00:45,990 so we know that we don't have an error text file to open. 8 00:00:45,990 --> 00:00:47,820 So we know that this is going to air out. 9 00:00:48,030 --> 00:00:49,380 So if we run this. 10 00:00:50,500 --> 00:00:57,610 We see that it panicked at with our air message air and then we see OS code four operating system code 11 00:00:57,610 --> 00:01:03,820 two kind not found and in the message the system cannot find the file specified. 12 00:01:04,120 --> 00:01:12,040 So we see that the error printed out an error kind of not found, which is information we can do something 13 00:01:12,040 --> 00:01:12,580 with. 14 00:01:13,630 --> 00:01:16,750 Air kind is provided by the standard library. 15 00:01:16,750 --> 00:01:20,140 So let's try to match on air kind. 16 00:01:20,140 --> 00:01:29,800 So what we can do is we can come up here and we can say use standard IO and then air kind, and now 17 00:01:29,800 --> 00:01:40,090 we can match on air kind so we can delete this and say Match air dot kind. 18 00:01:40,810 --> 00:01:50,440 And now we have another match statement in here so we can say air kind and then not found and then match 19 00:01:50,440 --> 00:02:02,440 on this and say, because we want to create a new file called Air Text, so we want to create air dot 20 00:02:02,440 --> 00:02:03,190 text. 21 00:02:03,190 --> 00:02:08,560 And then because this can also fail, we want to say, okay, file 22 00:02:11,020 --> 00:02:15,400 created and then just say file created. 23 00:02:16,600 --> 00:02:28,150 Otherwise we got an air here as well and we want to panic and it says cannot create the file. 24 00:02:30,110 --> 00:02:33,920 And now we have to have an exhaustive match. 25 00:02:33,920 --> 00:02:39,020 So down here we'll just do an underscore and say panic. 26 00:02:40,700 --> 00:02:48,260 It was some other air kind, which we know in this case it won't happen because we already know the 27 00:02:48,260 --> 00:02:49,760 air that we're going to get. 28 00:02:50,300 --> 00:02:51,980 So now if we run this. 29 00:02:55,020 --> 00:03:01,320 We then air out and we see that air act was created for us. 30 00:03:02,010 --> 00:03:02,550 So great. 31 00:03:02,550 --> 00:03:09,150 So that's how we can use match statements and we can even nest them to be able to handle logic for us. 32 00:03:09,750 --> 00:03:10,350 So. 33 00:03:11,250 --> 00:03:15,050 Another tool we have at our disposal is called Unwrap. 34 00:03:15,060 --> 00:03:15,840 So I'm going to. 35 00:03:17,560 --> 00:03:18,130 Comment. 36 00:03:18,130 --> 00:03:19,210 All of this out. 37 00:03:20,050 --> 00:03:24,370 And I'm actually going to just quickly rename this and just call it Air two. 38 00:03:24,400 --> 00:03:31,810 That way we can keep generating airs and that we can come up here and we can say unwrap and. 39 00:03:32,810 --> 00:03:38,510 What's going to happen in UNWRAP is if OC is returned from the method, then unwrap will return the 40 00:03:38,510 --> 00:03:41,300 value inside of oak forests. 41 00:03:41,570 --> 00:03:46,290 But if the result is an error, unwrap will call panic for us. 42 00:03:46,310 --> 00:03:52,820 So since we no longer have air, I expect this to panic force, which it does. 43 00:03:52,820 --> 00:04:02,330 And it said the thread main panicked at our unwrap unwrap method on an air value and then our normal 44 00:04:02,450 --> 00:04:03,410 air code. 45 00:04:04,010 --> 00:04:13,010 And then if we change this back to be a file that will be found, we no longer have an air. 46 00:04:13,790 --> 00:04:23,420 So if we want to customise our air, then we can use something called the expect. 47 00:04:23,660 --> 00:04:34,520 So instead of unwrap, we say expect and then we pass in a string air opening the file and then we get 48 00:04:34,520 --> 00:04:36,230 rid of these extra parentheses. 49 00:04:36,230 --> 00:04:43,940 So again, we know that this is going to air out because we changed our file name again. 50 00:04:44,930 --> 00:04:53,450 So now if we run this, we see that it says air opening the file and also gave us all the extra information 51 00:04:53,450 --> 00:04:54,470 regarding this. 52 00:04:54,470 --> 00:05:02,630 So expect is is beneficial because it's going to allow us to much more quickly track down the source 53 00:05:02,630 --> 00:05:04,910 of the panic call in any of our logs. 54 00:05:05,510 --> 00:05:10,340 So in the next lecture, we're going to look at how we can propagate air's.