Go error handling and custom error

Error handling:

Look at the piece of code: What to see output?

package main
import "fmt"

func test() {
  num1 := 10
  num2 := 0
  res := num1 / num2
  fmt.Println("res=",res)
}

main FUNC () {
  Test ()
  fmt.Println ( "The following code and logic ...")
}

The above summary of the code:

  1) By default, when the (panic) error occurs, the program will quit (crash)

  2) If we want to, when an error occurs, you can capture the error, ensure that the program can continue. You can also capture after an error, a prompt to the administrator (e-mail, SMS ...)

  3) here leads us talk about error handling mechanism

Basic instructions:

  1) Go pursuit of simple and elegant language, so, Go language does not support traditional try ... catch ... finally this treatment

  2) Go handled introduced to: defer, panic, recover

  3) the use of these unusual scene can be so simple description: Go can be thrown a panic, and then catch the exception by recover (built-in function) in the defer and then processed normally


Processed for the above error code: Using defer and recover to handle errors

Test FUNC () {
  // use defer + recover to capture and handle exceptions
  the defer FUNC () {
    // ERR: = Recover () // Recover () built-in function, can catch exceptions
    if err: = recover (); err! = nil {// description will trap
      fmt.Println ( "ERR =", ERR)
      // this can send error message to the administrator
      fmt.Println ( "E-mail to [email protected]")
    }
  } ( )
  num1: 10 =
  num2: = 0
  RES: = num1 / num2
  fmt.Println ( "RES =", RES)
}

main FUNC () {
  Test ()
  for {
    fmt.Println ( "main () and the following code logic ...")
    time.sleep (time.Second)
  }
}


Benefits error handling:

After the error handling, the program will not be easy to hang, if added early warning code, you can make the program more robust.

 

Custom error:


Go program also supports custom error, use errors.New and panic built-in functions.

  1) errors.New ( "Error Description"), returns the value of a type of error, error represents a
  2) panic built-in function to accept a value of type interface {} (i.e. the value of any) as a parameter. Acceptable error types of variables, output an error message and exit the program.

Case presentation:

package main
import (
  "fmt"
  "errors"
)

// function to read a configuration file information of init.conf
// If the file name passed is not correct, we will return a custom error
FUNC readConf (String name) (ERR error) {
  IF name == "config. INI "{
    // read ...
    return nil
  } the else {
    // return a custom error
    return errors.New (" error reading file ... ")
  }
}

Test02 FUNC () {
  ERR: = readConf ( "config2.ini")
  ! = nil IF ERR {
    // read the file if an error occurs, the error is output, and terminates the program
    panic (ERR)
  }
  fmt.Println ( "Test02 () to continue ... ")
}

main FUNC () {
  Test02 ()
  fmt.Println ( "Test02 () to continue ...")
}

 

Guess you like

Origin www.cnblogs.com/green-frog-2019/p/11355317.html