I think an equivalent question is - do all runtime errors, which may be fatal, panic? Because anything that panics should be recoverable. I'm not talking about recovering from things like os.Exit()
, or log.Fatal()
, or bugs in the Go runtime, or someone tripping over the power cord, but from other runtime errors which will lead to the program crashing.
Here's an example of a runtime error that can be caught via panic/recover:
package main
import (
"fmt"
)
func errorHandler() {
r := recover()
err := r.(error)
if err == nil {
return
}
fmt.Println(err.Error())
}
func foo() {
defer errorHandler()
smallSlice := []int{1, 0, 1}
smallSlice[10] = 1
}
func main() {
foo()
fmt.Println("recovery, end of main")
}
output:
runtime error: index out of range
recovery, end of main
Are there any examples where runtime errors will just crash the program without a recoverable panic?