Originally posted by: Venix
How is the C++ runtime throwing std::exception any different from Java's runtime throwing Exception?
Java can implement exceptions any way the underlying JVM wants, so long as the semantics of the try/catch block are maintained. One good way is to treat try/catch as no-ops, then look for them explicitly lower in the call stack when/if something goes wrong (e.g. an exception is thrown). C++ doesn't have that flexibility -- the only way to return to the try (in general) is to setjmp()/longjmp() (ignoring side effects, of course), which gives try/catch a pretty big runtime overhead in the common error-free case.
Hardware exceptions (access violations, divide by 0, priviliged instructions) are not handled by the standard C++ exception mechanism, so I'm not sure what you mean by the virtual memory comment.
Exactly! I was pointing out that, in unmanaged languages, the programmer must avoid or otherwise handle problems that aren't abstracted by 'exceptions'. That is, there is little to be gained by
adding exceptions to vanilla C++, as they cannot be used to handle
all failures, especially considering how much they complicate the memory model. My virtual memory comment was an example of a true hardware exception from which the C++ runtime does not recover. One cannot 'catch' SIGSEGV. (One
can recover from such a problem, if you want to install a segfault handler... it is therefore possible to make SIGSEGV catchable, but only with a setjmp())
C++ exceptions are thrown by operator new and the standard library; i.e. the C++ runtime.
Agreed. They're just objects. The throw keyword just wraps new and longjmp(). Its akin to a goto, or a fast POSIX signal. But as I said above, they're inferior to exceptions in higher languages, because one cannot 'catch' all error conditions.
The only major difference between C++'s and Java's exception mechanisms (besides forced checked exceptions) is that C++ allows sensible rollback and cleanup with RAII/ScopeGuard, whereas Java developers are stuck using the horrid try/finally garbage for cleanup and try/catch/rethrow for rollback.
I would say that RAII/ScopeGuard
add those features to disciplined C++, when/if they are used. They are not inherent features of C++ (sensible rollback? Bah!). If you're going to use disciplined C++, that opens the door to other ways of reporting/handling errors (e.g., like they do in C).
Incidentally, I certainly am not a fan of Java's try/catch/finally, either, but they make sense in a language that isn't intended to run native most of the time.
EDIT: Grammar