"Go" Panic function

Source: Internet
Author: User

Tag: Run call controller disappears user span absolute path ERR index

Panic (run-time panic) is an exception that will only be thrown back when the program is running. After panic is thrown, if no protection is added to the program, the program will print out the details of the panic and terminate the operation.

Give me a chestnut.

Package MainFunc Main () {    S1:= []int{01234}    e5:= s1[5]    = e5}

Running the above code will throw panic

out 1 [running]:   /users/haolin/geektime/golang_puzzlers/src/puzzlers/  Article19/q0/demo47.go:5 +0x3d  //Line code in its own source file in the number of lines, as well as the absolute path of the source file, +03d is the count offset, not very useful. 2/   /Exit Status Code 2 to end the run, the general state is not 0 indicates that the program is not normal exit 

What is the approximate process from the painc being thrown to the program terminating operation?

When a line of code in a function throws a panic, the initial panic details are built, and the program's controller immediately moves from this line of code to the line of code that invokes the function it belongs to (the previous level in the call stack), and the execution of the function that this code belongs to is terminated. Immediately, the control does not have a moment to stop here, it will immediately move to the previous level of the call code, the opposite direction propagation until the outermost function (go function, for the main goroutine is the main function). But the controller does not stay there, but is retracted by the Go language runtime system. Then the program runs and terminates, and the process of hosting the program will die and disappear. At the same time, during the controller propagation process, panic details are accumulated and perfected, and printed out before the program terminates.

// The main function calls the Caller1 function, and the Caller1 function calls the Caller2 function 1  /users/haolin/geektime/golang_puzzlers/src/puzzlers/article19/q1/demo48.go: +0x91  /users/haolin/geektime/golang_puzzlers/src/puzzlers/article19/q1/demo48.go: +0x66  /users/haolin/geektime/golang_puzzlers/src/puzzlers/article19/q1/demo48.go:9 +0x66   2

"Go" Panic function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: