How can I avoid using exceptions in C ++?

What methods can I use to avoid C ++ exceptions as stated in the Google Style Guide ?

+3
source share
6 answers
  • Do not throw exceptions.
  • Do not use STL (which is heavily dependent on exceptions).
  • Use only new(std::nothrow)or override ::operator newto return 0 on failure.

Note that by avoiding exceptions, you are effectively throwing away many useful libraries, including Boost. Basically, you have to program everything from scratch.

+8
source

: throw.

: new ( new(std::nothrow) malloc - ), , - , (, 0), operator new, - , .

, set_new_handler(), , litb.

, ++, . , , :

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

catch (...) ++ library_do_something ( output, ), , , 0.

, , RAII, , . RAII , , . - , . .

+3

, ++ , , .

, , RAII, , , , / ?

+2

, " ", - , (, new(std::nothrow) new, bad_alloc, .

+1

. , , .

, , , nothrow, () .

, , , , , , ...

In some cases, you can use exclusive versions of a version of some code (for example, throw new ones or not throw new ones).

0
source

What I do is never throw an exception with my own code and “translate” or package any external code that does this.

0
source

Source: https://habr.com/ru/post/1718152/


All Articles