In this video, learn how to panic when regular error handing is not enough. Discover how you can use defer and recover to catch panics and resume normal function execution.
- [Instructor] The usual method in Go for signaling errors is to return an error value. However, there are cases when there's an error you can't handle, and would like to signal this. In Go, there's a built-in panic function that will do just that. Let's cause a panic. We create a slice with three elements, and then we try to access the tenth element, which is out of bounds. When we run this code, we will see that we get panic, index out of range, and we will see where it happened, in line 10 in our code.
I mostly use panic when trying out code. This way, I get the error and the stack trace. However, when the code shapes up, I remove all panics from my code. Let's see an example. I'm going to comment out the section with the panic, and then file, err, equal os.Open no-such-file. And then if err is not nil, I'm just going to panic with a err.
If there was no error, I'm going to defer file.Close to make sure that the file is closed, and I'm going to print out that the file was opened. And of course, I need to import the os package. Let's run this code, go run panic.go. And we see panic, we can't open the file, and I know exactly the line number where the error happened. So here I define a function called safeValue.
It gets values, which is a slice of integer, and an index where I want to get the value from, and returns an integer. And we do a defer of a function, and this is an anonymous function, that gets no argument. And it checks, if there was an error, the built-in recover function, and the error was not nil, I'm going just to print out that there was an error. And what I'm going to do is try and return values at index.
Now let's call it. So I'm going to do v equal safeValue of a slice of integers with one, two, and three at index number 10. And I'm going to print what we get. Let's save it and run it. And we see our error being printed out, and we get the value zero. We got zero because this is the zero value for integers.
Since we didn't set value in our panic handler, we got zero value for integers. I usually use recover to wrap untrusted code, such as third-party libraries, which misbehave. The use of panic is discouraged in Go, so follow the Hitchhikers Guide to the Galaxy, and don't panic.
- Go basic types, conditionals, and loops
- Go functions
- Object-oriented programming with Go
- Error handling
- Concurrent programming with channels
- Testing and project management
- Networking with JSON and HTTP
Skill Level Beginner
Programming Foundations: Fuzzy Logicwith Erin Colvin41m 50s Intermediate
Getting started1m 34s
1. Getting Started
2. Go Basics
5. Error Handling
7. Project Management
Next steps1m 12s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.