Panic and recover

suggest change

This chapter assumes knowledge of defer.

In Go, panic and recover are technically similar to exception handling in languages like C#, Java, or Python.

panic is the equivalent of throw or raise, and recover fills the role of catch. recover only stops a panic when called directly from a deferred function.

However, while those other languages often use exceptions as a way to propagate errors up the call chain, using panic in Go is highly discouraged.

It should only be used in truly exceptional cases.

For ordinary error handling, read the chapter on error handling.

Panic

Recover

Recovering from panic

Use cases for panic

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents