Golang function four (error handling)

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

To facilitate the implementation of custom error types, the Go Language standard library defines error as an interface type. Like what:

Type listen to error listen interface{listen to listen to the error () listen to the string}

According to the Go Language programming habit, error is always the last function return value, and the standard library provides a CREATE function that makes it easy to create error messages. Like what:

Func Listen divtest (x listen, y listen int) (int, listen to error) {Listen to hear if listen to y hear = = Listen to 0{Listen listen listen listen to listen to hear hear 0, listen to errors. New ("Division Listen by") listen to//Create error message to listen to listen and listen listen to hear the return listen to hear x/y,nil}func listen to listen to the main () {Listen to hear V, listen to err listen to: = Listen to Divtest (3,0) Listen and listen if you listen to err listen to nil{listen to listen and listen to listen to the log. Fatalln (Err. Error ()) Listen and listen to listen and hear listen and hear println (v)}

In daily development, we need to customize the type of error based on requirements, can store more contextual information, or make appropriate error handling based on the type of error. Like what:

Type Listen Negativeerror listen to struct{listen to hear x, listen to Y listen to Int}func listen to (negativeerror) Error () string{listen to listen to hear "negative listen to value listen to error"} Type listen to Molerror listen to the struct listen {listen to listen to x, listen to Y listen to Int}func listen to (molerror) Error () string{Listen and listen to "devision listen to zero"}func listen to Moltest (x Listen, Y listen int) (int, listen to error) {Listen and hear if listen y listen = = Listen 0{Listen listen listen hear hear listen to listen to hear and hear} listen to listen to hear and listen} hear if listen to listen to listening to hear < listen to 0 listen to | | | Listen, listen, listen, hear, hear, listen to the 0{, listen, listen, hear and hear the return listen to the Listen, listen, hear, listen to the X%y,nil}func, listen to the negativeerror{x,y. Listen to hear if listen to err listen to listen to listen nil{Listen listen to listen to listen to hear switch E listen to hear: = Listen to err. Type) {Listen, listen, hear, hear, hear, listen, hear, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear Listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, hear, Fatalln (Err. Error ()) Listen and listen to hear println (v)}

Compared to error, Panic/recover is more similar to try/catch structured in applications. Like what:

Func Listen Panic () listen to interface{} Listen to the Func listen to recover () listen to interface{}

The difference: Panic immediately interrupts the current function processing flow and executes deferred calls. Recover can capture and return panic-generated error objects in deferred calls, such as:

Func Listen Myrecover () {Listen to hear if listen to err listen: = Listen to recover (); Listen to err listen! = Listen to nil{listen and listen to the log. Fatalln (ERR) Listen to listen to hear}func listen to the main () {Listen to listen to listen to println ("Start ...") Listen to listen to hear defer listen to listen to listen to Myrecover ("panic") listen to listen to Dead ("end ...} output: Start ... 2017/02/09 11:24:13 Listen to Deadexit listen to the status 1

If you have a scene that calls panic multiple times, only the last panic will be captured by recover, such as:

Func Listen Myrecover () {Listen to hear if listen to err listen: = Listen to recover (); Listen to err listen! = Listen to nil{listen and listen to the log. Fatalln (ERR) listen and listen to hear}func listen to the main () {Listen to listen to hear defer listen to Myrecover () listen to listen to the defer listen to Func () {Listen to listen to listen to listen to listen to the panic ("A listen to bad listen to problem") listen to listen to hear} () Listen to listen to panic ("a listen to Problem")} Output: 2017/02/09 Listen to the 11:31:50 listen to bad listen to problemexit listen to the status hear 1

Recover can only work properly in a deferred call function, such as:

Func listen to Main () Listen {Listen to hear defer listen to Myrecover () listen to listen to listen to defer listen to log. Println (Recover ()) listen to the defer listen to Println (Recover ()) Listen to the Panic ("a listen to Problem") listen to the output: (0x0,0x0) 2016/11/12 listen to 07:07:54 listen to <nil >2016/11/12 listen to a listen to problemexit listen to the status 1

In the daily development process, it is often necessary to debug, you can use the function to output the complete call stack information, such as:

Func Listen Myrecover () {Listen to hear if listen to err listen to: = Listen to recover (); Listen to err listen! = Listen to nil{Listen listen listen listen to the FMT. PRINTLN (ERR) Listen, listen, listen and listen to Debug. Printstack () Listen, listen, listen and listen to//log. Fatalln (ERR) listen to listen to}}func listen to the main () {Listen to hear defer listen to Myrecover () listen to hear panic ("a listen to Problem")} output: A listen to problemgoroutine listen to 1 listen [running]: Runtime/debug. Stack (0xc42002c010, listen 0xc42003fe20, listen to 0x1)/root/data/go/src/runtime/debug/stack.go:24 listen to +0x79runtime/debug. Printstack ()/root/data/go/src/runtime/debug/stack.go:16 listen to +0x22main. Myrecover ()/root/data/gopath/test/panic.go:10 listen to +0x85panic (0X48A5E0, listen to 0xc42000a320)/root/data/go/src/runtime/ panic.go:458 listen to +0x243main.main ()/root/data/gopath/test/panic.go:16 listen to +0x8d

In daily development, panic can be used only if the system is unrecoverable or not working correctly, such as port number is occupied, database is not started, file system error, etc., otherwise it is not recommended.

This article from "learned in the text, about the ceremony" blog, reproduced please contact the author!

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.