Brother Lian Blockchain training share go language-exception handling

Source: Internet
Author: User
Tags gopher

Blockchain is a complex and complex discipline that combines cryptography, consensus algorithms, intelligent contracts, and super-ledgers. Yun Jong said that the quality of the teachers in the blockchain training institutions and their perceived level in the blockchain industry were uneven, and the average consumer was not.


The go language is primarily used as a server-side development, targeting the development of "large software", suitable for many programmers to develop large-scale software together, and the development cycle of long-term support for cloud computing network services. The go language enables programmers to develop quickly, and it makes it easier for programmers to maintain and modify the software as it grows. It combines the efficiency of a traditional compiled language with the ease of use and expression of a scripting language.

Golang has many advantages, previous articles have also been mentioned, but there are many slots for Gopher, especially error handling.


Before you say a mistake or an exception, you should say two concepts:


Error handling: Errors are part of the business and can be foreseen.


Exception handling: Non-business part, unforeseeable.


Error handling

First look at the sample code:


File, err: = OS. Open ("/usr/local/test.txt")

Golang officially recommends error handling in the above code, and it is recommended that err be placed at the end of the return value. We also need to follow this rule to define Func in our daily coding.


There's a joke circulating between Gopher: Half the time you write code, and half the time you write error handling.

Example code:


Func Open (name string) (*file, error) {

Return OpenFile (name, o_rdonly, 0)

}

Usually, however, not every method needs to handle err, which can be properly returned to the upper function, uniformly printed by the upper function, or handled incorrectly. For example, an error in an HTTP route can be processed before the route returns data, and the error message and error code are formatted and returned to the client.


Exception handling

Golang exception handling is maverick, need defer err recover () Three to use, and Java as long as try{}catch () can be done, or look at the sample code:


Package Main


Import (

"FMT"

)


Func Main () {

Test ()

}


Func Test () {

Defer func () {

If e: = Recover (); E! = Nil {

Fmt. Println ("worng!")

}

}()

Panic ("Panic")

}

If a panic error occurs in the test () function and its child functions, the above code will print:


worng!

Code Encapsulation

Of course, it is not elegant to write such a large stack of redundant code at the top of each method, and it does not conform to the object-oriented nature: encapsulation, so it can be encapsulated into covererrormessage (), and the test () function is rewritten as follows:


Package Main


Import (

"FMT"

)


Func Main () {

Test ()

}


Func Test () {

Defer tools. Covererrormessage ()

Panic ("Panic")

}


Func Covererrormessage () {

If message: = Recover (); Message! = Nil {

var err error

Switch x: = message. (type) {

Case string:

Err = errors. New (x)

Case ERROR:

Err = X

Default

Err = errors. New ("Unknow Panic")

}

Logger.error ("Recovered panic Error:", err)

}

}

Defer can only write recover () in the first layer function when handling exceptions, otherwise it will not recover () to panic error


high-energy early warning, brother even education blockchain live course August continues to hit the hot!

The original price of 1188 Yuan of 12 block chain advanced courses, now only need 1 Yuan!

Also can receive free "Go Language foundation actual combat project development" and "Go language Advanced Combat Project development" textbook Two!!

Limited time limit!! First come first served!!

Http://www.ydma.cn/open/course/24


Pay attention to Brother chain training can get more Blockchain technology dry!!!


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.