Go Fast Learning-10-defer and exception handling

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

Directory

      • Defer and exception handling
        • Overview
        • Defer
          • Define multiple Defer
          • Change the return value
        • Panic and recover
          • Using panic
          • Using recover

Summary

Defer,defer stack, change the return value, Panic,recover

Defer and exception handling

Overview

    • Go does not have try..catch this anomaly mechanism, but uses panic and recover .
    • panicCan be executed anywhere, recover only in the function that is defer called.

Defer

    • deferUsed to define statements executed at the end of a function execution
    • Multiple defer will be formed defer 栈 , and later defined defer statements will be called first
    • Even if the function has a critical error, it will execute
    • You can modify the result after a return by cooperating with the anonymous function

Define multiple Defer

func testDefer() {    fmt.Println("a")    defer fmt.Println("b")    defer fmt.Println("c")    deferfunc() {        fmt.Println("d")    //a d c b

Change the return value

Since defer executes at the end of the function execution, it can be used to change the value of the named return value

Incorrect use of

funcintint {    deferfunc() {        //返回的值,改变变量没有用    }()    return x}x = changeResult(8)fmt.Println("x"//x 8

The right usage

funcintint) {    deferfunc() {        result++    }()    return x}x = changeResult2(8)fmt.Println("x"//x 9

Panic and recover

    • panicUsed to pass an exception up, defer followed by a very severe non-recoverable exception that causes the program to hang
    • recoverCan be panic captured to make it panic stop passing up

Using panic

func one() { fmt.Println("func one") }func two() { fmt.Println("func two") }func broken() {    one()    panic("func broken with Panic")    two()}broken()

Output

func onepanicfunc broken with Panic异常消息...程序中断

Using recover

func one() { fmt.Println("func one") }func two() { fmt.Println("func two") }func catch() {    one()    deferfunc() {        ifrecovernil {            fmt.Println("func catch with Recover")        }    }()    panic("func broken with Panic")    two()}catch()

Output

func onefunc catch with Recover

Attention

    • deferNeed to be placed panic before the definition
    • recoverAfter that, the logic does not revert to panic that point, and the function will defer return after
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.