C++ - Arguments for Exceptions over Return Codes

后端 未结 11 1668
鱼传尺愫
鱼传尺愫 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:33

    It is very hard to write exception safe code. A completely contrived example is :-

    void Class::Method()
    { 
      i++;
      SomeCallThatMightThrow();
      j++;
    }
    

    Replace i++ and j++ with any two variables, resources, states that must remain synchronous. Garbage collection saved us from having to remember to pair our new's and deletes. Ironically, old fashioned explicit return code testing saves us from having to carefully analyse every function that might throw exceptions to check that they havn't screwed with the post-conditions.

提交回复
热议问题