How to Create Custom Errors using New Function in Golang?

Custom errors are those that can be defined by the user. For example, if one writes a function that is used to divide two numbers, which is to be used in multiple places, the user could create a custom error that can be set whenever division by zero is encountered, for it could prove to be fatal in the long run.

The easiest way to create custom errors in Golang is to use the New() function which is present in the errors module of Golang. Before actually moving on to creating our own error, let’s have a look at how it is implemented in the errors package.

filter_none

edit
close

play_arrow

link
brightness_4
code

package errors
  
// New returns an errorString
// that contains the input 
// text as its value.
func New(text string) error {
    return &errorString{text}
}
  
// errorString is a trivial
// implementation of error.
type errorString struct {
    s string
}
  
func (e *errorString) Error() string {
    return e.s
}

chevron_right


So, basically what the New() function does is create an error with a custom message that can be written by the programmer. For example, let’s say we want an error to be displayed whenever a negative value is given as input for the program that outputs the area of a square, given its side.

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
// the errors package 
// contains the New function
import (
    "errors" 
    "fmt"
)
  
func main() {
    side := -2.0
    if side < 0.0 {
      
            // control enters here if error condition is met
        fmt.Println(errors.New("Negative value entered!")) 
        return
    }
    fmt.Printf("Area= %f", side*side)
  
}

chevron_right


Output:

Negative value entered!

This was so because we had entered the length of side as -2, which cannot be possible, and hence the error was thrown. Now let’s try out a similar program but this time let’s put the error generation in a separate function.

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import (
    "errors"
    "fmt"
)
  
func findAreaSquare(side float64) (error, float64) {
    if side < 0.0 {
  
        // function that returns the error
        // as well as the computed area
        return errors.New("Negative value entered!"), 0
    }
    return nil, side * side
}
func main() {
    side := -2.0
    err, area := findAreaSquare(side)
    if err != nil {
        fmt.Printf("%s", err)
        return
    }
    fmt.Printf("Area= %f", area)
  
}

chevron_right


Output:

Negative value entered!



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.