3 ms·
> If you need to recover from bad slice indexes then your [error handler] code is broken You bring up a good point that when resorting to value comparisons for
by 0xABADC0DA 15y ago
> If you need to recover from bad slice indexes then your [error handler] code is broken
You bring up a good point that when resorting to value comparisons for error handling instead of type comparisons it's easy to make mistakes. The code is as far as I can tell a simplest way to handle an index out of bounds, but to also handle a 'slice out of bounds' it would need to also compare to the string value "runtime error: slice bounds out of range" -- not helping the case for error handling in Google Go.
This code strikingly shows deficiencies in Google Go non-local error handling:
- Tons of boilerplate (defer, recover, re-panic, type check, value check)
- Not scoped so can only handle an error once per function
- Have to do value tests in addition since types are very generic due to implicit interfaces
- Error values are poorly defined
- Result for higher-level caller is buried deep in the function and non-obvious
These problems extend to all non-local error handling in Google Go, not just for this specific case of array and string indexes. That the idiomatic way to 'handle' an error is to abort the program is another separate problem.