Re:Error stack traces in Go with x/xerror
by simiones on Tuesday August 24, 2021 @06:45AM
(#28287050)
The whole point of Exceptions is to avoid writing try/catch all over the place. Let's take an example of idiomatic exception-based code:
func doRequests(url1, url2 string) resp throws HttpException {
resp1 := http.DoRequest("POST", url1)
resp2 := http.DoRequest("GET", url2 + resp1.ID)
return resp2
}
vs func doRequests(url1, url2 string) (resp, error) {
resp1, err := http.DoRequest("POST", url1)
if err != nil {
return nil, fmt.Errorf("Error in req1: %w", err)
}
resp2, err := http.DoRequest("GET", url2 + resp1.ID)
if err != nil {
return nil, fmt.Errorf("Error in req2: %w", err)
}
return resp2, nil
}
Of course, we can write the second example with try/catch as well, but the whole point of exceptions is to be able to write the first one when appropriate.> The whole point of Exceptions is to avoid writing try/catch all over the place
Of course. I'm talking about when you actually handle your exceptions you should be specific about the lines you're handling.
You just need to have some thought when writing your code: