In C++ what are the benefits of using exceptions and try / catch instead of just returning an error code?

后端 未结 13 886
遥遥无期
遥遥无期 2020-12-04 19:58

I\'ve programmed C and C++ for a long time and so far I\'ve never used exceptions and try / catch. What are the benefits of using that instead of just having functions retur

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 20:39

    • return an error code when an error condition is expected in some cases
    • throw an exception when an error condition is not expected in any cases

    in the former case the caller of the function must check the error code for the expected failure; in the latter case the exception can be handled by any caller up the stack (or the default handler) as is appropriate

提交回复
热议问题