ZeroCool
ZeroCool

Reputation: 1500

What do you prefer: C++ exception handling or passing a buffer to hold error messages as parameter?

I am currently involved in developing a low level network application. We have lately run into the issue of error reporting concerning both user land and debug land. Given that some errors might fire up from lower level functions, libraries ... reporting the exact error status (code, messages, env...) from one layer to another higher layer has revealed to be somewhat cumbersome.

However we have thought to switch back to using exceptions but recently a pal has came out with this approach : Using a class Error_Buf which holds sufficient informations about what causes the error as such error code, buffer message, and passing it as a parameter in functions. Hence whenever something goes wrong, an adequate message and error code are set in error->buffer and error->error_code for example and then are forwarded back to the calling function and an adequate return value is sent.

This is actually what we use to do in c (at least libnet does something similar) but how is this near or far from efficiency and robustness, code maintainability might be an issue too.

Thank you,

Upvotes: 3

Views: 409

Answers (3)

doron
doron

Reputation: 28932

I prefer error codes, this is because, as a programmer, you are forced to think of errors at every level of code and handle them appropriately. This might a simple error occurred here trace entry.

With exceptions, by the time one catches them one is often quite far removed from the source of the problem. If the problem results from a genuine bug, one is often too far removed to track it down effectively.

Upvotes: 0

Alex F
Alex F

Reputation: 43331

Exceptions handling is much better, it allows to keep code clean and short, without testing every function for success/failure and return on failure. It has runtime cost, if you want something super-fast, think about C-style error handling.

It is possible to use combined approach, when some low-level time-critical functions use return values, and all other code uses exceptions.

Upvotes: 2

sbi
sbi

Reputation: 224149

The advantage of exceptions is that callers cannot ignore them. OTOH, I've seen so much code that ignores return values.

Also, in some code, if you check all calls for possible errors, the algorithm will be buried under error handling code. With exceptions, that's not an issue.

Upvotes: 8

Related Questions