tags:

views:

136

answers:

3

Possible Duplicate:
C++ - Arguments for Exceptions over Return Codes

While you are doing C++ programming, you have two choices of reporting an error. I suppose many teachers would suggest you throw an exception, which is derived from std::exception. Another way, which might be more "C" style, is to return a non-zero value, as zero is "ERROR_SUCCESS".

Definitively, return an exception can provide much more information of the error and recovery; while the code will bloat a little bit, and making exception-safe in your mind is a little difficult for me, at least. Other way like returning something else, will make reporting an error much easier; the defect is that managing recovery will be a possibly big problem.

So folks, as good programmers, which would be your preference, not considering your boss' opinion? For me, I would like to return some nonzero values.

+1  A: 

You might find Exception handling seems to make my life more difficult; clearly I'm not the problem, am I?? worth reading.

JRL
+1 enjoyed the link
John Dibling
+2  A: 

From the C++ FAQ: Use Exceptions for errors.

The logic behind this in my opinion, is that with return codes, the onus is up to the caller of your code to check return codes to handle errors. This then, allows users of your code to potentially get into a bad situation. Exceptions, on the other hand, must be dealt with, or they bubble up the call stack until it terminates the program.

In my opinion, if you are writing C++ (and wish to adhere to C++ idioms), and want to use error handling, exceptions are the way to go.

Alan
A: 

Error handling is much easier through exceptions most of the time. Writing exception safe code is also quite easy. I'm lazy. I take the easy route.

Noah Roberts