Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions vs. errno

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?

like image 692
Philip Avatar asked Dec 07 '22 21:12

Philip


2 Answers

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.

like image 178
Conrad Meyer Avatar answered Dec 25 '22 01:12

Conrad Meyer


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.

like image 30
Andrew Avatar answered Dec 25 '22 01:12

Andrew