Introduction to Golang Custom Errors
In the go language in case if some exceptions occur then the program automatically displays the error those errors are generally defined by the go language. But what if we wanted to display error other than the system return so we have the idea of the custom error. In the custom error we will be able to send errors according to our way or error which is more understandable to the end-users, to return error either we can use the error package like errors.new(msg) or fmt.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, type of occurring of the errors, and any custom message if you want, etc. We need to give correct data type for the attribute and if given string as 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 going to understand the working of the custom errors we need to understand why we need the custom errors. Suppose you are calculating the area of any circle and to calculate the area 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 number 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 end-user instead of directly showing error 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 which we wanted to return to the calling functions.
- We can return two attributes for the error like return result, error. Here result will be the outcome and error is the error that occurs while calculating the result.
Examples of Golang Custom Errors
In the below we have given some of the important examples for the custom error in the go language in the below examples we are showing error with predefined attributes and error other then what generated by the go language. In case if we wanted to execute the below examples then we can create a file with 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 –