C++ - Arguments for Exceptions over Return Codes

后端 未结 11 1640
鱼传尺愫
鱼传尺愫 2020-11-29 21:10

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 -

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 21:50

    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.

提交回复
热议问题