Which do you prefer: handling C ++ exceptions or passing a buffer to store error messages as a parameter?

I am currently involved in the development of a low-level network application. Recently, we have encountered the problem of error messages both for the userโ€™s land and for debugging. Given that some errors can be triggered from functions of a lower level, libraries ... report that the exact error status (code, messages, env ...) from one layer to another higher level showed that it is somewhat cumbersome .

However, we decided to return to using exceptions, but recently a friend came up with this approach: Using the Error_Buf class, which contains enough information about what causes an error such as an error code, a buffer message, and passing it as a parameter to a function. Therefore, whenever something goes wrong, an adequate message and error code are set, for example, to error-> buffer and error-> error_code, and then they are sent back to the calling function and the corresponding return value is sent.

This is actually what we use in c (at least libnet does something similar), but how close or far is it from efficiency and reliability, code support can be a problem too.

Thank,

+3
source share
3 answers

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

In addition, in some code, if you check all calls for possible errors, the algorithm will be buried under the error handling code. With exceptions, this is not a problem.

+8
source

Exception handling is much better, this allows you to keep the code clean and short without testing each function for success / failure and return on failure. It has runtime, if you want something superfast, think about C style error handling.

, , .

+2

I prefer error codes because, as a programmer, you are forced to think about errors at each level of the code and handle them accordingly. This can lead to a simple error in the trace record.

With exceptions, by the time they catch them, it is often quite far from the source of the problem. If a problem arises due to a genuine error, it is often too far away to effectively track it.

0
source

Source: https://habr.com/ru/post/1765125/


All Articles