3 ms·
This is why I have grown to hate expressing errors as exceptions. Nowadays, I handle errors one of two ways: it's either part of the return value of the functio
by titzer 4y ago
This is why I have grown to hate expressing errors as exceptions. Nowadays, I handle errors one of two ways: it's either part of the return value of the function (i.e. an error state, usually a case of a variant), or the caller passes an error handler object, with the default behavior is the error handler records a list of things that went wrong. Threading the error handler through generally isn't too bad; then you can check it whereever you want and handle it at the top level. It's almost impossible to swallow an error unless you create a new error handler object and throw it away, which would be a very rude thing to do.
This works especially well for parsing data, as you can write most of the parser completely oblivious of previous errors and keep on truckin'. The top-level parse routine will surface the first lexicographical error. The code is very clean but still gives really good error messages (and multiple! I used this for the Virgil parser and entire compiler).