Programming languages generally have exception trapping mechanisms , stay Python in It's using raise and try-except Statement to achieve exception throw and exception capture .

stay Golang
in , There are a lot of routine mistakes , It can be alerted in advance during the compilation phase , Such as grammatical errors or type errors , But some errors can only occur after the program is running , For example, array access is out of bounds , Null pointer reference, etc , These runtime errors cause the program to exit .

Of course, it can trigger the shutdown and exit of the program , It can be us , For example, through inspection and judgment , When the current environment does not meet the expected conditions for our program ( For example, a service specifies that the listening port is occupied by other programs ), It can be triggered manually
panic, Let the program exit and stop running .

<>1. trigger panic

Manually trigger downtime , It's a very simple thing , Just call panic This built-in function is enough , It's like this
package main func main() { panic("crash") }
After operation , Direct error reporting and downtime
$ go run main.go go run main.go panic: crash goroutine 1 [running]: main.main()
E:/Go-Code/main.go:4 +0x40exit status 2
<>2. capture panic

An exception has occurred , Sometimes you have to catch it , It's like Python Medium except equally , that Golang How did China do it ?

This leads to another built-in function – recover, It allows programs to be reborn after a crash .

however recover Use of , There is one condition , It has to be in the defer Function , Under other scopes , It doesn't work .

This is a simple example
import "fmt" func set_data(x int) { defer func() { // recover()
You can capture the panic Information printing if err := recover(); err != nil { fmt.Println(err) } }() //
Deliberately creating array out of bounds , trigger panic var arr [10]int arr[x] = 88 } func main() { set_data(20) //
If we can carry out this sentence , explain panic Caught // Subsequent programs can continue to run fmt.Println("everything is ok") }
After operation , The output is as follows
$ go run main.go runtime error: index out of range [20] with length 10
everything is ok
Generally speaking , It's not right to enter panic The down program does anything , But sometimes , We need to be able to recover from downtime , At least we can do that before the program crashes , Do something about it , for instance , When web
When the server encounters unexpected serious problems , All connections should be closed before crashing , If nothing is done , This will keep the client waiting , If web
The server is still in development , The server can even feed back the abnormal information to the client , Help debug .

<>3. Cannot cross collaboration

From the example above , You can see , even if panic Will cause the entire program to exit , But before we quit , If so defer Delay function , Still have to finish defer .

But this one defer There is no effect between multiple coroutines , Trigger in the subprocess panic, You can only trigger the defer, It cannot be called main In the process defer
Functional .

Just do an experiment
import ( "fmt" "time" ) func main() { // this defer It will not be enforced defer fmt.Println("in
main") go func() { defer println("in goroutine") panic("") }() time.Sleep(2 *
time.Second) }
The output is as follows
in goroutine panic: goroutine 6 [running]: main.main.func1()
E:/Go-Code/main.go:12 +0x7b created by main.main E:/Go-Code/main.go:10 +0xbc
exit status 2
<>4. To sum up

Golang Exception throwing and catching , Depends on two built-in functions :

* panic: Throw an exception , Crash the program
* recover: Catch exception , Resume procedures or close out work
revocer After calling , Thrown out panic It will end here , No more throwing , however recover, It can't be used arbitrarily , It has mandatory requirements , It has to be in defer
Only by doing so can we make full use of it .

Technology