"Go" Panic function

Source: Internet
Author: User
Tags terminates


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 main

func main() {
    s1 := []int{0, 1, 2, 3, 4}
    e5 := s1[5]
    _ = e5
}


Running the above code will throw panic


Panic: runtime error: index out of range

Goroutine 1 [running]: //The goroutine with an Id of 1 is running when this panic is raised
Main.main()
  /Users/haolin/GeekTime/Golang_Puzzlers/src/puzzlers/article19/q0/demo47.go:5 +0x3d //The number of lines in this line of code, and the absolute path of the source file, +03d is the count Offset, not very useful.
Exit status 2 //End the operation with exit status code 2, the general status is not 0, indicating that the program exits abnormally.



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.
Goroutine 1 [running]:
Main.caller2()
  /Users/haolin/GeekTime/Golang_Puzzlers/src/puzzlers/article19/q1/demo48.go:22 +0x91
Main.caller1()
  /Users/haolin/GeekTime/Golang_Puzzlers/src/puzzlers/article19/q1/demo48.go:15 +0x66
Main.main()
  /Users/haolin/GeekTime/Golang_Puzzlers/src/puzzlers/article19/q1/demo48.go:9 +0x66
Exit status 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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.