1 00:00:05,860 --> 00:00:10,300 Russ tries to help us out when it comes to handling errors in our programs. 2 00:00:10,480 --> 00:00:16,660 Firstly, Russ is going to group errors into two categories recoverable and unrecoverable. 3 00:00:16,990 --> 00:00:22,720 Recoverable errors are going to rely on the result type, and then unrecoverable errors are going to 4 00:00:22,720 --> 00:00:24,310 use the panic macro. 5 00:00:24,340 --> 00:00:27,190 So let's start off by looking at the panic macro. 6 00:00:27,520 --> 00:00:31,300 The panic macro is going to terminate the current thread. 7 00:00:31,300 --> 00:00:40,060 So let's terminate our main thread and we'll say panicked here. 8 00:00:42,240 --> 00:00:49,920 And now we'll run this and we can see that it says thread Maine panicked at panicked here which was 9 00:00:49,920 --> 00:00:52,890 our error message or our panic message. 10 00:00:52,890 --> 00:01:00,450 I should say one example that would cause a panic to occur is if we try to perform an out of bounds 11 00:01:00,450 --> 00:01:01,770 access on array. 12 00:01:02,590 --> 00:01:04,620 So let's look at that real quick. 13 00:01:04,630 --> 00:01:08,680 So let's create a vector 14 00:01:11,170 --> 00:01:13,600 and let's just put in a value of one. 15 00:01:13,840 --> 00:01:20,020 And now let's say that we want to access index ten at the vector for whatever reason. 16 00:01:20,410 --> 00:01:25,750 So obviously the ID does not signal signal that this is an error. 17 00:01:25,840 --> 00:01:27,250 But when we run this. 18 00:01:28,480 --> 00:01:32,890 It says thread main panicked at index out of bounds, which is right here. 19 00:01:33,250 --> 00:01:35,260 The length is one, but the index is ten. 20 00:01:35,260 --> 00:01:38,080 So it was able to give us a good error message. 21 00:01:38,080 --> 00:01:39,550 But again, this. 22 00:01:40,310 --> 00:01:43,550 Index out of balance caused a panic. 23 00:01:44,000 --> 00:01:51,500 So now we see that it says note run with rest underscore backtrace equals one environment variable to 24 00:01:51,500 --> 00:01:53,000 display a backtrace. 25 00:01:53,120 --> 00:01:57,050 So let's do that real quick and I'm doing this on Windows. 26 00:01:57,050 --> 00:02:02,420 So for me to set the environment variable, I. 27 00:02:03,360 --> 00:02:07,710 Run this command right here to set Russ back trace equal to one. 28 00:02:07,950 --> 00:02:15,270 So now that I have it and we run cargo run again, we see that I have the backtrace printed out. 29 00:02:18,510 --> 00:02:25,470 The Backtrace is basically just going to give us a list of all the functions in a recursive order. 30 00:02:27,130 --> 00:02:30,490 Leading up to our air and to the program. 31 00:02:30,490 --> 00:02:31,480 Unwinding. 32 00:02:31,990 --> 00:02:36,040 So unwinding is the default behavior for handling a panic. 33 00:02:36,220 --> 00:02:41,020 The first thing we get is an error message printed to the terminal in this case. 34 00:02:41,380 --> 00:02:43,750 Then the stack starts to unwind. 35 00:02:43,780 --> 00:02:50,410 During this process, temporary values, local variables, current function arguments, etc. are all 36 00:02:50,410 --> 00:02:53,110 dropped in the reverse order they were created. 37 00:02:53,980 --> 00:02:59,650 Once the current function is cleaned up, it moves on to whoever called the function that panicked and 38 00:02:59,650 --> 00:03:02,590 performs the same unwinding process. 39 00:03:02,710 --> 00:03:07,190 Then eventually, after all, the unwinding is completed, the thread exits. 40 00:03:07,210 --> 00:03:13,060 If the panicking thread was the main thread, the whole process exits, which is what we did in this 41 00:03:13,060 --> 00:03:13,660 case. 42 00:03:14,790 --> 00:03:21,420 There is another behavior that panicking can have and that is called an abort in the event of an abort. 43 00:03:21,450 --> 00:03:24,130 Unwinding does not happen. 44 00:03:24,150 --> 00:03:31,890 For example, imagine we called a customized drop method during the unwinding process, but this drop 45 00:03:31,890 --> 00:03:33,780 method also panics. 46 00:03:33,960 --> 00:03:37,980 This would be considered fatal and causes the whole process to stop. 47 00:03:38,730 --> 00:03:41,040 And that's really all there is to it, to panics. 48 00:03:41,040 --> 00:03:48,480 So now that we know how panics work, let's look at air handling that we can manage with recoverable 49 00:03:48,480 --> 00:03:49,080 errors.