Check out a free preview of the full Go for JavaScript Developers course:
The "Panic, Defer & Recover Practice" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna goes over a practice of the different error handling methods in Go: what a function should defer to when there is an error, when a function should panic, and how a function can recover from a panic.

Get Unlimited Access Now

Transcript from the "Panic, Defer & Recover Practice" Lesson

[00:00:00]
>> And lastly for our error handling section here I wanna talk about the two panic and defer functions you saw a little bit in that last example. When we were talking about opening and closing a file, you saw that if the file is successfully opened, we can call defer, and then f.Close.

[00:00:15] So a couple of reasons why this is a really cool function. A lot of times like once we open a file, there's gonna be a lot of stuff we want to do with it. Let's say you're parsing it, logging it, whatever you're trying to do with the file you just opened and a lot of that stuff happens before you wannaa close it.

[00:00:30] But sometimes, especially with readability, it's nice to see some of these things closer together. So you see that on the first line, you're opening a file, and then at some point, you're gonna defer this till later. You wanna close that file in the middle. You can do whatever you want.

[00:00:41] So the defer line is gonna happen last. And then panic at the bottom there is just a review of, if you wanted to throw a panic error that's gonna completely halt execution of your program, you can use the method panic and then call whatever you want within that.

[00:00:54] In this case we're just telling it to go find that error that was just thrown. So let's hop back into errors ago and talk about defer. Okay, so let's talk about what's happening in this code snippet, where we have a set of different functions in our do things method.

[00:01:21] But the first thing that happens is in function main, where we call our do things function. And if you hop up to do things, you'll see that the first two lines have the the defer method here. And then stuff happens to the right hand side where we're just printing some lines.

[00:01:36] So I run this right now. We are going to be up in errors.go. You'll notice that the order of operations are kind of the opposite of what you would see if you were looking at just like line by line execution of this code. So the first thing that happens is things and stuff should happen first switch is executed on line 54.

[00:01:54] The second is do this second to last which happens on line 53, and then first line but do this last, which happens at line 52. And so what's cool about defer, is that it's gonna keep going through the defers in a last in first out type of structure.

[00:02:08] So the first thing it's gonna do it's gonna be like cool, I see a bunch of defers, let's skip through those, find the first line of execution that I can do first, which is this last line on 54. And then it's gonna kinda work its way up and execute each of the defers kind of as it reverses its way through the code.

[00:02:25] So, so far we've kinda talked about panic and defer and the third member of the party here is another method called recover. And so panic is gonna be called if the application kinda stops execution and you need to actually like fataly do something different within your code, defer is going to tell your code to kind of hang out and do something differently.

[00:02:44] And if you pair all these together with the recover verb, you're gonna tell go what to do, if your application panics. So recovering differ kind of like buddies, where if recover is paired with defer, it's gonna be like okay, once you panic, go check for any statements closing a file, even if the file itself doesn't exist or you do something with it and it blows up.

[00:03:05] You still wanna close that file before the end of the function, so panic and differ work really well together. But if there's more you wanna do besides just close that file like hey, update a log or do something else. You can call recover, to be, okay, something panicked, divert some stuff and also do this as well.

[00:03:22] And so we're gonna look at kind of what that looks like. Back in Pericycle. Cool. So you'll notice that in main, we still have our do things function. But this time we've modified the do things to have a little bit more going on. So first of all, we're calling a for loop or iterating over a set of indexes and then we're printing the index.

[00:04:09] And if I was to, we're just gonna panic. And so we're gonna call this panic function. We're gonna throw the string a panic. And see what happens if we run, do things as is. You'll see we get zero, one and two and then we panic. So even though our for loop should iterate over to five.

[00:04:31] Once I set to two, we panic. But let's say once we panic, we want to alert the user that something happened be more specific, add a little bit more detail. Let's add the defer and recover, to add a little bit more functionality to our code here. Cool. So within our do things function, we're going to first set up a defer.

[00:04:57] So the last thing that should fire here, is a function that we're gonna make an a second call to recover from panic. So even if the function panics, go find a defer if one exists and execute that code So let's write a function called recover from panic. Okay, so we're gonna modify our do things function just a little bit we have our for loop that iterates over our index up until five and then if index hits two, we panic.

[00:05:32] And so when we call panic, we know that the first thing is gonna do is look for any defer functions. And if there is a defer function, that kind of fire, whatever those instructions are. So I'm gonna create a function called recoverFromPanic which is instructions for what this program should do if the program panics.

[00:05:49] And then recoverFromPanic here, we're gonna start implementing the recover function. So to use the recover function, we're gonna first instantiate a variable called R and set that to whatever recover returns and recover is only going to return a value if there has actually been a panic. So similar to how we had our error handling, both define the variable and a check for a value.

[00:06:14] We can say, Hey, if you called recover, and the recover value is not nil. So recover actually got a value back, do something with that information. So here I'm just going to print line and say something like, hey, we panicked, but everyone's fine. And then after that, let's also print line.

[00:06:43] And I wanna kind of demonstrate like what instructions have to be just to see what did our recover function get if it's not nill what is it. So in here, let's just print R. Cool. So now that we have a recover from panic function written, Cool. We can do one last thing.

[00:07:12] So right now when we panic, we're simply passing it a string. If I were to run the code as is, We are in section eight for errors. We're gonna see we panicked but everyone's fine. And then we see that the instructions that were sent to a recover function is the string that we actually handed our panic.

[00:07:37] And this is kind of a cool way to pass some information around your program. Even if something goes wrong, what do you wanna do about it? So instead of just halting execution completely, let's add a few more layers of instruction to handle it a little more gracefully.