Skip to content

Latest commit

 

History

History
74 lines (65 loc) · 2.36 KB

Error_Handling.md

File metadata and controls

74 lines (65 loc) · 2.36 KB

Error Handling

Error handling is a crucial aspect of writing reliable and robust software. In GoLang, error handling is done in a way that encourages clear and explicit handling of errors, making your code more readable and maintainable.

Handling Errors Using the Error Interface

GoLang uses the error interface to represent errors. An error is a simple interface type with a single method: Error().

Creating an Error

You can create an error by implementing the error interface in a custom type. This is usually done using the errors package.

import(
 "errors"
)
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

Handling an Error

When calling a function that returns an error, it's important to check if the error is nil before proceeding.

result, err := divide(10, 0)
if err != nil {
    fmt.Println("Error:", err)
    return
}
fmt.Println("Result:", result)

Custom Error Types

In addition to using the errors.New function, you can create custom error types by implementing the error interface methods in your own types.

type MyError struct {
    Message string
}

func (e MyError) Error() string {
    return e.Message
}

func doSomething() error {
    return MyError{Message: "Something went wrong"}
}

Using Panic and Recover for Exceptional Cases

In exceptional cases where a program cannot continue to execute normally, GoLang provides the panic and recover mechanism. However, it's recommended to avoid using panic for normal error handling.

Panic

The panic function stops the normal execution of the current goroutine. It's often used to indicate unrecoverable errors.

func doSomething() {
    if somethingBadHappened {
        panic("Something bad happened")
    }
}

Recover

The recover function can be used to regain control of a goroutine after a panic. It's typically used in deferred functions to clean up resources.

func handlePanic() {
    if r := recover(); r != nil {
        fmt.Println("Recovered from panic:", r)
    }
}

func main() {
    defer handlePanic()
    doSomething()
}
  • Error handling is a fundamental part of writing reliable and maintainable GoLang code. Using the error interface, custom error types, and the panic and recover mechanisms, you can effectively manage errors and handle exceptional cases in your programs.