Exceptions Against Errno

As a C programmer, I don't have much experience with exceptions. I'm rather used to errnoas a means of passing errors in multiple function calls. That, having said, I do not see the distinguishing feature of exceptions, therefore ...

What is the main difference between exceptions and usage errno?

+3
source share
6 answers

There are so many differences that it’s hard to say where to start.

, errno, C, ; , , errno -setting, errno , . , errno .

++ , , . , ; . ++ , , errno.

+3

errno. .

, :

try {
   // something
}
catch( ... ) {
   // nothing
}
// continue as if nothing happened

(Java)

try {
   // something
}
catch( Throwable t ) {
   // nothing
}
// continue as if nothing happened

, , , - .

+3

, . , , " " . exception.

+3

1) , . , .

2) , , errno, , , , . , , , . . , .

+2

, errno , - , ... , (, ) , .

, , , , . .

+2

, , - , , -, , . , - :

  pkt_type = tcp_getbyte(my_stream, timeout);
  pkt_length = tcp_getbyte(my_stream, timeout);
  pkt_length |= tcp_getbyte(my_stream, timeout) << 8;
  if (pkt_length < MAX_PACKET_LENGTH)
  {
    for (i=0; i<pkt_length; i++)
      buffer[i] = tcp_getbyte(my_stream, timeout);
  }
  if (!my_stream->error)
  {
    /* Do something with packet */
  }

If one attempt to get a byte timeout, subsequent attempts will fail unconditionally, returning zero. There is no need to check each operation for failure; if something goes wrong, the system will behave as if tcp_getbyte () had thrown an exception, just not so fast.

+1
source

All Articles