How can I avoid using exceptions in C++?

后端 未结 6 1780
臣服心动
臣服心动 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:20

    I'm interested to know why one would want to avoid exceptions in C++ and what mechanism one would replace them with to deal with the reality of unexpected failure while still maintaining decent structure.

    Sure adding them to a existing codebase that doesn't use RAII type semantics is extremely costly - but if one is doing green field development then what alternative would you suggest and how are going to justify not using high quality libraries that do use exceptions vs. writing your own exception free / bug free alternatives?

    0 讨论(0)
  • 2021-02-04 21:31
    1. Don't throw exceptions.
    2. Don't use STL (which relies heavily on exceptions).
    3. Use only new(std::nothrow) or override ::operator new to return 0 on failure.

    Note that by avoiding exceptions, you're effectively throwing out lots of useful libraries, including Boost. Basically, you'll have to program everything from scratch.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-02-04 21:43

    What I do is to never throw exceptions with my own code, and to "translate" or wrap any external code that does that.

    0 讨论(0)
  • 2021-02-04 21:44

    Not throwing exceptions in your own code is relatively easy: you just don't use the throw statement.

    Not throwing exceptions from memory allocation failures is a little more painful: either you don't use normal new (use new(std::nothrow) or malloc or something instead), or you use some nonstandard compiler option to get it to do something nonstandard when it fails (e.g. immediately terminate your program, or return 0), or you override operator new to do something nonstandard when it fails.

    If your chosen approach is to immediately terminate the program, you can implement this with set_new_handler(), which I had forgotten about until litb reminded me.

    That leaves the problem of dealing with exceptions generated by C++ libraries you don't maintain. Generally you'll have to wrap library calls in a wrapper that looks something like this:

    int DoSomething(int &output, const int input) throw() {
      try {
        output = library_do_something(input);
        return 1;
      } catch (...) {
        return 0;
      }
    }
    

    The catch (...) catches all possible C++ exceptions from library_do_something (as well as the assignment operator on output, which isn't relevant here), throws away all the information they may have contained, and then maps all those failures to 0.

    Note that this style means that you can't use RAII at all, not even a little bit, because you have no way of signaling failure within a constructor. The whole point of RAII is that you acquire all your resources inside of constructors so that they will be properly released by a destructor during exception propagation. But resource acquisition is something that can essentially always fail. So you can't do that inside a constructor.

    0 讨论(0)
  • 2021-02-04 21:46

    The style guide says they "don't use exceptions" which is just that - they don't throw them and don't call anything that could throw them (for example, they would use the new(std::nothrow) instead of usual new because the latter will throw bad_alloc when it fails to allocate memory.

    0 讨论(0)
提交回复
热议问题