Brother even blockchain training go language panic and recover (catch exception)

Source: Internet
Author: User
Tags square root

With the "Subversion storm" caused by the blockchain, a large number of blockchain training institutions emerged. But in a mixed-up training circle, it is bucket to find a truly standard course system and a dedicated professional lecturer in the Blockchain field. Brother even education pointed out that it is time to make action to change and subvert the traditional training institutions operating thinking, and remind the public users, should be rational choice of blockchain training institutions.

Func Panic (interface{}) and Func recover () interface{} are the two functions used for error handling in Golang.


The function of panic is to throw an error message, and from its argument type it can be seen to throw any type of error message. When panic is called somewhere in the execution of the function, an error message is immediately thrown, and the normal execution of the function terminates, but the defer statement panic previously defined in the function is executed sequentially. The goroutine immediately stops executing.


Recover () is used to capture information from panic. Recover must be defined in the defer statement before panic. In this case, when panic is triggered, the goroutine will not simply terminate, but will execute the defer statement defined before it.


The following is a simple example:


--Capture your own set of panic errors:


Package Main


Import "FMT"

Import "Math"


Func foo (a int) {

Defer FMT. Println ("foo exits")


Defer func () {


If r: = Recover (); R! = Nil {

Fmt. Printf ("Caught Error:%s\n", R)

}

}()


If a < 0 {


Panic ("must enter a number greater than 0")

}


Fmt. Println ("The root of the number is:", Math.) Sqrt (Float64 (a)))


}


Func Main () {


var a int

A = 10

Fmt. Printf ("a=%d\n", a)

Foo (a)


var b int

b =-10

Fmt. Printf ("b=%d\n", b)

Foo (b)


Fmt. Println ("The goroutine can also be executed")

}

// ///////////

a=10

The square root of the number is: 3.1622776601683795

Foo's out of here.

B=-10

Caught error: Must enter a number greater than 0

Foo's out of here.

The Goroutine can also perform


Process finished with exit code 0


----


--Captures panic errors inside the Go language:


Package Main


Import "FMT"


Func foo () {


Defer func () {


If r: = Recover (); R! = Nil {

Fmt. Printf ("Caught Error:%s\n", R)

}

}()


var a, b int


A, B = 1, 1

c: = 3/(A-B)

Fmt. Println (A, B, c)


}


Func Main () {

Foo ()

}


//====

Caught error: Runtime Error:integer divide by zero

"

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.