How can I avoid using exceptions in C++?

后端 未结 6 1787
臣服心动
臣服心动 2021-02-04 21:08

What techniques can I use to avoid exceptions in C++, as mentioned in Google\'s style guide?

6条回答
  •  被撕碎了的回忆
    2021-02-04 21:31

    In some compilers, you may be able to turn off exception handling. This could cause unexpected results with external code though - it is not something I would want to try.

    Other than that, the obvious first step would be to avoid throwing exceptions from your own code, probably with liberal use of the nothrow directive, and attempt to avoid throwing exceptions from external (3rd party) code through defensive programming.

    That means that your code needs to be aware of possible external exceptional failure conditions all the time, such as running out of memory, out of disk space, loss of an internet connection, hardware failure, and any number of other circumstances that might cause code to throw...

    In some cases you may be able to use exception-free versions of some code (such as throwing-new vs. non-throwing new).

提交回复
热议问题