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

后端 未结 13 879
遥遥无期
遥遥无期 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:27

    The advantage of exceptions are two fold:

    • They can't be ignored. You must deal with them at some level, or they will terminate your program. With error code, you must explicitly check for them, or they are lost.

    • They can be ignored. If an error can't be dealt with at one level, it will automatically bubble up to the next level, where it can be. Error codes must be explicitly passed up until they reach the level where it can be dealt with.

提交回复
热议问题