Alex Rivera | Logout

C++ - Arguments for Exceptions over Return Codes

Asked 2009-12-04T20:34:21.507
37

I'm having a discussion about which way to go in a new C++ project. I favor exceptions over return codes (for exceptional cases only) for the following reasons -

  1. Constructors can't give a return code
  2. Decouples the failure path (which should be very rare) from the logical code which is cleaner
  3. Faster in the non-exceptional case (no checking if/else hundreds of thousands of times)
  4. If someone screws up the return code settings (forgets to return FAIL) it can take a very long time to track down.
  5. Better information from the message contained in the error. (It was pointed out to me that a return enum could do the same for error codes)
  6. From Jared Par Impossible to ignore without code to specifically designed to handle it

These are the points I've come up with from thinking about it and from google searches. I must admit to being predisposed to exceptions having worked in C# for the past couple of years. Please post further reasons for using exceptions over return codes. For those who prefer return codes, I would also be willing to listen to your reasoning. Thanks

Edit
Report

1 Answer

3

Faster in the non-exceptional case (no checking if/else hundreds of thousands of times)

In the non-exceptional case, it's a single comparison to determine that E_SUCCESS was returned.

If someone screws up the return code settings (forgets to return FAIL) it can take a very long time to track down.

If someone fails to check the exceptions, it can be difficult to notice until you actually get an exception thrown there. If you're dealing with error codes, you know just by looking at it whether they're checking for them or not.

answered 2009-12-04T20:39:13.823

Your Answer