3 ms·
> destructors invoked via exceptions can't throw their own exceptions `finally`, in languages that have it (and presumably, scope(exit) too), face fundamentall
by deathanatos 9y ago
> destructors invoked via exceptions can't throw their own exceptions
`finally`, in languages that have it (and presumably, scope(exit) too), face fundamentally the same problem with exceptions that destructors do: if an exception is already propagating, and you throw another one, what happens?, since you (presumably) can't have two exceptions. C++ says tough / don't do that / you're terminated. Other languages just forget about the other exception, and you lose the context (which can be a pain to debug; though I think more recent versions of Python, for example, will attach the original exception as a "context" to the new one) but your program continues execution.
Languages that expose a result object, e.g., Result in Rust, force you to deal with the issue yourself, essentially, by deciding at the site what to do with it. (The downside being the increased amount of code, though to some extent I believe the language can supply tooling to help there; Rust is again a good example. The upside is you get to choose what happens.)
(I see these more as a tradeoff, that different languages have different opinions about.)
- deleted 9y ago[deleted]
- cjalmeida 9y agoJava from the beginning allows you to chain exceptions and that is a core part of debugging. In Python this is a very recent construct and few libraries use it, unfortunately.