Error handling of Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Error handling

Error

The go language introduces a standard error-handling pattern, the errors interface, which is defined as follows:

Type Error Interface {    error () string}

For most functions, if you want to return an error, you can use error as the last of the multiple return values:

func foo (param int) (ret int, err error) {  ...  }

Code at the time of invocation:

N, Err: = foo (0) If err! = Nil {    //   error handling} else {    // use return value n}   

We can also customize the error type, an example:

Package main import "FMT"Import "errors"// custom error structure type myerror struct {    arg  int    errmsg String} Implement the Error interface func (E *myerror) error () string {return FMT. Sprintf ("%d-%s", E.arg, e.errmsg)}// Two kinds of error func error_test (arg int) (int, error) {if arg < 0 {Retu Rn-1, errors. New ("Bad arguments-negtive!" )}else if arg >256 {return-1, &myerror{arg, "Bad Arguments-too large!" }} return arg*arg, nil}// related test func main () {for _, I: = range []int{-1, 4, +} {if r, E: = Error_ Test (i); E ! = nil {fmt. Println ("failed:", E)} else {fmt. Println ("Success:", R)             }} 

Defer

You can add multiple defer statements to the GO function, and when the function executes to the end, the defer statements are executed in reverse order (that is, the last defer statement will be executed first), and finally the function returns.

Especially when you are doing some open resources operation, encountered errors need to return early, before returning you need to close the corresponding resources, otherwise it is easy to cause problems such as resource leakage. As shown in the following code, we generally write open a resource that works like this:

Func CopyFile (DST, src String) (w Int64, err error) {    Srcfile, err: = os. Open (SRC)    if err ! = Nil {        return     }    defer srcfile.close ()    dstfile, err: = os. Create (DST)    if err ! = Nil {        return     }    defer dstfile.close ()    return IO. Copy (Dstfile, Srcfile)}   

You can also use an anonymous function if the defer after a statement does not finish the cleanup work:

Defer func () {    ...} ()


Note that thedefer statement is executed after the return , for example:

Func test () (result int) {    defer func () {        result = ()    } ()    return}func main () {    fmt. PRINTLN (Test ())     ///  

Panic () Recover ()

The panic () function is used to throw exceptions, and the Recover () function is used to catch exceptions, and the prototypes of these two functions are as follows:

Func Panic (interface{}) func recover () interface{}

When panic () is called in a function, the normal function execution process terminates immediately, but statements in the function that were previously deferred by the DEFER keyword are executed normally, after which the function returns to the calling function and causes the panic () process to be executed step-by-layer. Until all functions that are executing in the owning goroutine are terminated. The error message is reported, including the parameters passed in when the Panic () function is called, which is called error process processing.

Panic () accepts a interface{} parameter and can support any type, for example:

Panic (404) Panic ("Network Broken") Panic (Error ("File Not exists"  )

In the defer statement, you can use recover () to terminate the error-handling process, which avoids an exception being passed up, but note that after recover (), the program does not go back to panic () and the function is returned after the defer.


Func foo () { Panic (errors. New ("I ' m a Bug")) return}func Test () (result int) { defer func () { if r: = Recover (); r! = Nil { ERR: = R. (Error) FMT. Println ("Cache Exception:", Err)}} () foo () return}func main () {FMT. PRINTLN (Test ())//0}

Note that when panic is called in a function, its defer statement is still executed,

int {    defer        func () {if r: = Recover (); r! = Nil {            n+ +    // take effec tive        }    } ()    n+ +             // take effective    Panic (errors. New ("i ' m a bug"))    n+ +             / take no effective     return  n}

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.