4 ms·
C++ exceptions are dangerous, even when working as expected. In my opinion, C++ without exceptions (or the minimal for e.g. logging the error -division by 0, in
by faragon 9y ago
C++ exceptions are dangerous, even when working as expected. In my opinion, C++ without exceptions (or the minimal for e.g. logging the error -division by 0, invalid memory access, etc.-) is a fine language.
- xorblurb 9y agoNote: there is no such thing as standard C++ exceptions on invalid memory accesses. I'm not sure about division by 0, might be in the same case.
- faragon 9y agoStandard or not, if you use an OS with memory protection (MMU), you'll get those exceptions in C++.
- lmm 9y agoWithout exceptions you can't do RAII (because you can't do nontrivial work in constructors, because you have no way to report failure) which is probably C++'s one real selling point. (There are other reasons I'd say it isn't a fine language - mainly the lack of sum types and the way templates are only checked after full expansion (that one will at least be fixed if and when concepts make it into the language)).
- nathell 9y agoRAII used to be incompatible with exceptions (I'm not sure whether anything changed for the better in C++17). The reason is that you can't throw exceptions from destructors, and e.g., on Linux, destruction of the file object (wrapping a file descriptor) can result in an error because close() may well return -1 setting errno to EIO. See `man 2 close` for details.
- tcbawo 9y agoThat's really not true. It takes a little discipline to ensure your constructor will succeed prior to calling it, but it's worth it. You generally want to make your destructor noexcept, so you get free implicit move optimizations.