alessandro
alessandro

Reputation: 1721

Error Handling in Go

I have a go code like this,

main()
{
    do something
    do something
    .
    .
    .
    do something
}

Now, I don't know which "do something" is throwing an error. Is it possible in Go to catch the error and print it? How?

Upvotes: 1

Views: 406

Answers (2)

Arpssss
Arpssss

Reputation: 3858

Go language did not include exception handling mechanism. However, it has panic/recover mechanism which gives a little bit of exception handling.

Upvotes: 2

Matt Joiner
Matt Joiner

Reputation: 118470

You probably want recover. Alternatively, check the return values from those functions. It's idiomatic in go to call the error value ok, and immediately check it.

meh, ok := do_something()
if !ok {

Upvotes: 2

Related Questions