3 ms·
Java with try-with-resources does the correct thing: It attaches the new exception as a secondary exception to the currently in-flight exception. Since functio
by layer8 2y ago
Java with try-with-resources does the correct thing: It attaches the new exception as a secondary exception to the currently in-flight exception.
Since function calls form a tree, exceptions must form a tree as well.
Doing this automatically is also one of the killer arguments for exceptions over error codes, IMO.
- dwattttt 2y agoHow automatic it is depends on the language, but error "objects" (rather than codes) can do this too. It is pretty great.
- layer8 2y agoYes, with enough syntactic sugar it can become equivalent. Exceptions can be viewed as sum types with special syntactic sugar in conjunction with the regular return types, and can in principle be implemented as such. When I say "exceptions", I mean the source-level semantics, not how it's implemented behind the scenes.
- kokada 2y agoGo also has `errors.Join` since 1.20, and one of its uses is exactly this one: to be used in deferred close that can possible raise errors so the errors can stack each other: https://stackoverflow.com/a/78013962 https://stackoverflow.com/a/78013962. > Doing this automatically is also one of the killer arguments for exceptions over error codes, IMO. Definitely doing this automatically is better than relying on the programmer to do this manually, but I wouldn't say this is the "killer" argument for exceptions over errors codes, because this doesn't add anything new to the argument of exceptions vs explicit error handling.