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开发者_C百科 efficiency and robustness, code maintainability might be an issue too.
Thank you,
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.
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.
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.
精彩评论