4 ms·
> I get being against exceptions in a language like C++ where it can leave your program in a really weird state If you're programming in modern C++, then excep
by deathanatos 12y ago
> I get being against exceptions in a language like C++ where it can leave your program in a really weird state
If you're programming in modern C++, then exceptions allow you cleanly (as in, clean code) leave your program in a very well defined state. You have to adhere to RAII, and there's a lot of C++ shops out there that don't allow RAII (and are wary of exceptions, because they ignore RAII). I've routinely used exceptions in C++ as a both efficient and clean way to propagate errors out to a stack frame that can handle them, fully confident that RAII will clean up as I go.
> and have some ugly performance implications
More than having an error object that the calling frame needs to check? If I understand Go (and, since it's mentioned here and I think is relevant, Rust's) setup, each stack frame between the frame that can handle the error and the one where the error occurred must check that an error did indeed occur, and if so return it. Rust makes this pretty through macros, and the article has some good thoughts on it for Go. Nonetheless, can the compiler compile that to better than an `if error { return error }` type check?
My understand of C++ exceptions is that this was basically how things _were_ done (but no longer). Previously, exceptions were managed by "sjlj" (short for setjmp, longjmp, the calls used to manage exceptions); any frame that _could_ have an exception would need to call setjmp on entry, and this was a bit hefty.
However, my understanding is that, at least for g++ on Linux, C++ has abandoned this in favor of a newer scheme called "dwarf2"[1]; this scheme is such that entering a "try" (or a stack frame where RAII would need to do actual cleanup) is free (rather, it is covered as part of the normal stack setup, really), leaving it gracefully is also free; only during actual stack unwinding do exceptions add code/performance penalties.
I think there is an unfortunate amount of information that C++ exceptions are inefficient left over from the sjlj days.
[1] http://www.x86-64.org/documentation/abi.pdf http://www.x86-64.org/documentation/abi.pdf
- Rusky 12y agoEntering a try with newer implementations of exceptions is free in that it doesn't execute anything extra, but it's not free. It inhibits the optimizer and creates a lot of duplicated code in the binary, as well as forcing exception safety on the programmer, which is pretty hard to achieve completely, efficiently, and/or cleanly.
- overgard 12y agoYeah, I mean, that can work, but I feel like the phrase "in modern C++" always has the hidden caveat of "Unless you're stuck with a bunch of legacy junk". A lot of times you end up having to use legacy code you can't trust. Your stuff is RAII, but this old legacy code you have to link with... and nobody wants to rewrite it... you probably get the point. I'm not against C++ exceptions, I just think there are legit reasons for shops to avoid it in that particular language . But I like exceptions in general.