3 ms·
> Exception error handling is implicit because we don't know which line inside a try block can throw an exception You just need to have some thought when writi
by vips7L 5y ago
> Exception error handling is implicit because we don't know which line inside a try block can throw an exception
You just need to have some thought when writing your code:
let a;
try {
a = someFailingFunc();
} catch (e) {
// handle
}
// use a
// do work
- simiones 5y agoThe 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.
- vips7L 5y ago> 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.