Updated May 30, 2023
Introduction to Golang Custom Errors
In the go language, if exceptions occur, the program automatically displays the error. Those errors are generally defined by the go language. But what if we wanted to display an error other than the system return so we know the custom error? In the custom error, we can send errors according to our way or error, which is more understandable to the end-users. We can use the error package like errors. New (msg) or fmt to return errors.ErrorF(msg,attributes). In this, we can pass the object of the attribute, which can contain the error details. In this topic, we are going to learn about Golang Custom Errors.
Syntax
Below is a simple syntax for the custom error; here, we are returning the errors, and the errorConfig is any variable you can give any name to it. This variable contains the attributes with data types of the attribute. These attributes are used to place whatever we want to keep inside the error message, like why the error happens, the type of occurrence of the errors, and any custom message if you wish to, etc. We need to give the correct data type for the attribute, and if the given string is the data type, then we need to place string data.
Please see the below syntax for a better understanding.
type errorConfig struct {
attribute1 data-type
Attribute2 data-type
}
return 0, &errorConfig{message,value}
How do Custom Errors work in the Go language?
Before understanding the working of the custom errors,, we need to know why we need the custom errors. Suppose you are calculating the area of any circle, and to calculate the size of any circle, we need the area of the circle, as we need the area of the circle must be some positive value so we can show some custom errors for better understanding. In the same way, we can take another example. Like suppose we have to perform the division and we have two numbers, and our division will be a/b,, and by mistake, the value of the b will be 0. In such case,, the error will happen,, and stop the execution of the program. So with the help of the custom errors, we can show some more user-friendly errors to the end-users instead of directly showing errors generated by the compiler of the go language.
We can give some important points on the custom errors in the go language.
- We can use errors and fmt. Sprintf to return the error.
- We can create a hash like an attribute and all the attributes we want to return to the calling functions.
- We can return two attributes for the error: return result and and error. Here result will be the outcome,, and the the error is the error that occurs while calculating the result.
Examples of Golang Custom Errors
Below, we have given some important examples of the custom error in the go language. In the examples below, we show errors with predefined attributes and errors other than what is generated by the go language. If we want to execute the examples below, we can create a file with a name error. Go and copy and paste the below examples on the file, and we can run the command go run error.go, and we can get the output of these files.
Example #1
Please see the below example, along with the screen of the output.
Code:
//Importing all the packages needed for the below example
package main
import (
"errors"
)
import (
"fmt"
)
import (
"math"
)
//Creating a function to calculate the shape of the circle or area by radius
func getShapeDimension(r float64) (float64, error) {
if r < 0 {
return 0, errors.New("The calculation are failed, because the radius is negative value")
}
var x =math.Pi * r * r
return x,nil
}
func main() {
r := -24.0
//Catching the result or error whichever will come from the function
value, error := getShapeDimension(r)
//Checking if the function returning anything as the error then handle it
if error != nil {
fmt.Println(error)
return
}
fmt.Printf("The dimension is", value)
}
Output:
Example #2
Please see the below example along with the screen of the output.
Code:
//Importing all the packages needed for the below example
package main
import (
"fmt"
)
import (
"math"
)
//Creating a function to calculate the shape of the circle or area by radius
func getShapeDimension(r float64) (float64, error) {
if r < 0 {
return 0, fmt.Errorf("The calculation of are failed, because the radius is negative value")
}
var x =math.Pi * r * r
return x,nil
}
func main() {
r := -24.0
//Catching the result or error whichever will come from the function
value, error := getShapeDimension(r)
//Checking if the function returning anything as the error then handle it
if error != nil {
fmt.Println(error)
return
}
fmt.Printf("The dimension is", value)
}
Output:
Example #3
Please see the below example, along with the screen of the output.
Code:
//Importing all the packages needed for the below example
package main
import (
"fmt"
)
import (
"math"
)
//Defining the custom attributes for the error message which we will display
type shapeError struct {
issue string
r float64
}
func (err *shapeError) Error() string {
return fmt.Sprintf("radius %0.2f: %s", err.r, err.issue)
}
//Creating a function to calculate the shape of the circle or area by radius
func getShapeDimension(r float64) (float64, error) {
if r < 0 {
return 0, &shapeError{"The calculation of are failed, because the radius is negative value",r}
}
var x =math.Pi * r * r
return x,nil
}
func main() {
r := -24.0
//Catching the result or error whichever will come from the function
value, error := getShapeDimension(r)
//Checking if the function returning anything as the error then handle it
if error != nil {
if error, success := error.(*shapeError); success {
fmt.Printf("The Radius of shape is %0.2f which is negative", error.r)
return
}
fmt.Println(error)
return
}
fmt.Printf("The dimension is", value)
}
Output:
Conclusion
From this tutorial, we learned the basic concept of the custom error in the go language, we learned about the simple syntax, and the working principle of the custom error in the go language. We also focus on some of the important examples of custom errors.
Recommended Articles
This is a guide to Golang Custom Errors. Here we discuss the introduction, syntax, and working of custom Errors in the Go language, along with the programming examples. You may also have a look at the following articles to learn more –