2 ms·
I agree. A function that properly handles its helpers internal errors is better than a function that simply rethrows them. However, both are better than a funct
by lalaithion 5y ago
I agree. A function that properly handles its helpers internal errors is better than a function that simply rethrows them. However, both are better than a function that simply rethrows them AND leaves the application in an inconsistent state. Additionally, adding the "try" syntax makes it clear that "insert" has been written poorly. For example, consider the code:
func insert(id: Int, object: ThingInSpace) {
var location = try get_location(object);
ids.update(id);
locations[id] = location;
metadatas[id] = compute_metadata(object);
}
Now I can be sure that compute_metadata doesn't fail, so it's fine that I've inlined the call, whereas it's important that get_location isn't inlined. But this syntax also suggests to a code reviewer that maybe insert should wrap the error returned by get_location and caught.
Of course you could always catch the error and restore the state, but without the `try` syntax, you can't tell by reading the function which lines can fail, and where you need to handle the error.