Golang Error Handling error_php tutorial

Source: Internet
Author: User

Error handling errors of Golang


There is no exception-handling mechanism in Golang that can only rely on the return value to make a state error (of course it has a panic/recover mechanism, but generally only handles unexpected errors).

For a function's return value, the Convention is that the last parameter returns an Error object to indicate the state of the function's run.
Such as:
    1. N, Err: = Func ()
    2. If err! = Nil {
    3. ....//process Error
    4. }
or write it together.
    1. If n, err: = Func (); Err! = Nil {
    2. ....//process Error
    3. }

The error object can be made by errors. New () or FMT. Errorf () construction.
Such as:
    1. var dividederr = errors. New ("Cant divided by 0")
Or
    1. ERR: = FMT. Errorf ("%d cant divided by 0", Arg)

Let's start by looking at what the error is.
Error is defined as an interface type in the standard library, which has only one error () method:
    1. Type Error Interface {
    2. Error () string
    3. }
In other words, the custom structure only needs to have the error () method, which is equivalent to implementing the error interface.


We can create a struct and implement the error () method to construct the error object according to our own wishes.
Such as:
 
  
  
  1. Type Division struct {
  2. ARG int
  3. STR string
  4. }
  5. Func (e *division) Error () string {
  6. Return to FMT. Sprintf ("%d%s", E.arg, E.str)
  7. }
  8. Func Dividecheck (arg1, arg2 int) (error) {
  9. if arg2 = = 0 {
  10. Return &DIVISION{ARG1, "can ' t divided by 0"}
  11. }
  12. return Nil
  13. }

Another example is to check if there is a set of data that cannot be removed (that is, the divisor is 0), and if so, an error is returned.
The code is as follows:
 
  
  
  1. Package Main
  2. Import "FMT"
  3. Func Dividecheck (arg1, arg2 int) (error) {
  4. if arg2 = = 0 {
  5. Return to FMT. Errorf ("%d can ' t divided by 0", arg1)
  6. }
  7. return Nil
  8. }
  9. Func Main () {
  10. var err error
  11. Err = Dividecheck (4, 2)
  12. If err! = Nil {
  13. Fmt. PRINTLN (ERR)
  14. Return
  15. }
  16. Err = Dividecheck (8, 0)
  17. If err! = Nil {
  18. Fmt. PRINTLN (ERR)
  19. Return
  20. }
  21. }

We implemented this feature, but the code is very elegant, with at least 3 lines of error handling per function call execution.

below to optimize. The function we need to implement is to return an error whenever there is a number that cannot be removed. Then you only need to store the state after each check in the internal state variable, and then check the variable after all processing is done.
The code is as follows:
 
  
  
  1. Package Main
  2. Import "FMT"
  3. Type Division struct {
  4. Err error
  5. }
  6. Func (this *division) Dividecheck (arg1, arg2 int) {
  7. If This.err! = Nil {
  8. Return
  9. }
  10. if arg2 = = 0 {
  11. This.err = FMT. Errorf ("%d can ' t divided by 0", arg1)
  12. Return
  13. }
  14. }
  15. Func (this *division) ERR () error {
  16. Return This.err
  17. }
  18. Func Main () {
  19. D: = new (Division)
  20. D.dividecheck (4, 2)
  21. D.dividecheck (8, 0)
  22. If D.err ()! = Nil {
  23. Fmt. Println (D.err ())
  24. }
  25. }
This code is much more elegant, and before each check to determine whether the internal state has been wrong, the error is immediately returned, almost no performance loss.


Golang's error handling is often criticized, but if you know how to program it in go, you can do it gracefully.

http://www.bkjia.com/PHPjc/1114325.html www.bkjia.com true http://www.bkjia.com/PHPjc/1114325.html techarticle Golang error Handling Errors Golang There is no try/catch such an exception handling mechanism, can only rely on the return value to do State error judgment (of course, it has a panic/recover mechanism, but generally only ...)

  • 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.