#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Error Handling

Error Handling in Go – Custom Errors, Panic, and Recover Guide

The error Type

Go handles errors explicitly as return values, rather than using exceptions.

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

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

Custom Errors

type NotFoundError struct {
    Name string
}

func (e *NotFoundError) Error() string {
    return e.Name + " not found"
}

panic and recover

panic stops normal execution immediately, while recover can catch a panic inside a deferred function to prevent the program from crashing.

func safeDivide(a, b int) {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from:", r)
        }
    }()
    fmt.Println(a / b) // panics if b is 0
}

Ready to master real-world Go skills?

Learn Go hands-on with mentor-led, live sessions.

Explore Course