As a C programmer, I don't have much experience with exceptions. I'm rather used to errno
as a means of communicating errors across several function calls. That having said, I don't see the distinguishing feature of exceptions, so...
What is the fundamental difference between exceptions and using errno
?
There are so many differences here it's hard to say where to start.
First of all, errno
as used in C is a global variable; this means every routine that calls an errno
-setting subroutine must check errno
before performing any other work, if it cares about correctness. Luckily enough, errno is threadsafe.
C++ exceptions automatically unwind up the call stack until they find a function prepared to handle the fault. This means that in most cases users don't have to explicitly check every call for an error; instead they can collect error returns in one place. C++ exceptions can contain values other than integers, unlike errno.
You can casually ignore errno. Exceptions must be dealt with.
Of course I've seen my share of:
try {
// something
}
catch( ... ) {
// nothing
}
// continue as if nothing happened
and (Java)
try {
// something
}
catch( Throwable t ) {
// nothing
}
// continue as if nothing happened
BUT at least that kinda jumps out at you when you're plowing through someone else's mess.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With