Error Handling

230 views
Skip to first unread message

Rich

unread,
Feb 7, 2023, 4:38:41 PM2/7/23
to golang-nuts
In most of my code I create a function to handle errors. looks something like this:

func errorHandle(err error, str string, ex bool) {
     if err != nil {
          fmt.Printf("error: %s -- %v\n",str, err)
     }
     if ex {
        os.Exit(1)
     }
}

This cleans up my code:
    inFile, err := os.ReadFile("Mydata.txt")
    errorHandle(err,"read mydata.txt",true) // The true will exit

So you see that I can exit the program when there is an error, what I want to know is if it's possible to do the same thing inside a function, to where I don't exit the program but return from the function:

func OpenFile( filename string) []byte {
    inFile,err := os.ReadFile(filename)
    errReturn(err, "read "+filname,true)

When there is an error it would return from the OpenFile function,

Axel Wagner

unread,
Feb 7, 2023, 4:45:43 PM2/7/23
to Rich, golang-nuts
No, that is not possible. Only a `return` statement can return.
You *can* unwind the stack, using `panic` or `runtime.GoExit` (which is what `t.Fatal` does) but it's a bad idea in general (and FTR, I'd also consider your function a bad idea, but that's none of my business).

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2eec0130-3deb-4f60-a13e-d7c9d132771dn%40googlegroups.com.

Richard Masci

unread,
Feb 7, 2023, 5:01:53 PM2/7/23
to Axel Wagner, golang-nuts
You said: "and FTR, I'd also consider your function a bad idea, but that's none of my business" <- I'll be the first to say not all my ideas are good ones, maybe this one isn't? Thanks for your response.

ben...@gmail.com

unread,
Feb 9, 2023, 5:10:11 PM2/9/23
to golang-nuts
I agree with Axel that a function like errReturn is a bad idea, though if Go updates/improves error handling, it's possible we'll get something like that (some of the error handling proposals have been similar to errReturn).

However, I don't see anything wrong with a function like Richard's errHandle(), *if it's at the top level in main*. I often have a function like this in main.go for CLIs. The signature might be:

inFile, err := os.ReadFile("Mydata.txt")
exitOnError(err, "cannot read file")

-Ben

Harri L

unread,
Feb 12, 2023, 1:24:03 PM2/12/23
to golang-nuts
A package err2 helps you to implement the `errReturn` even though Go doesn't have macros or can return from the function enclosing the function checking the error.
Please see the playground that does exactly that:

However, I recommend you to use `err2` with all of its power, i.e., automation. Could you look at the following playground? Even the error strings/messages are autogenerated from function names (yeah, localization, but in most cases, enough to start with). Declarative error handling helps to keep your code safe.

And yes, I'm the author of the err2.
Reply all
Reply to author
Forward
0 new messages