Friday, 15 April 2011

boost - C++ try-catch exception handling conventions -


This may be a trivial question - but it was not clear to me, so I need some confirmation. Let me have a try-block in the function:

  int function some_crap () {some_type b; Int a = 10; {A = boost :: numeric_cast & lt; Int & gt; (B); } Hold (boost: bad_numeric_cast & amp; e) {std :: cout & lt; & Lt; E.what () & lt; & Lt; Std :: endl; Return; } Hold (...) {// - handle other unknown exceptions; } Return; }   

IIRC, I need to return the statement in each block block, right? In addition, each hold block, a will have value in 10 , right?

Thank you for your time & amp; Interest. - T

IIRC, I will have to return each Catching statement given in the block, right?

No, not in this case. The important thing is that you do not flow till the end of your return-return function without having to return anything (up to 6.6.3 / 2 will be undetermined behavior).

Since you have a return statement: return;

At the end of your work, your hold block does not need to include one.

In addition, if you do not have any values ​​to return to the case of an error, you can restore the currently handled exceptions:

  Throw ;   

Or to avoid catching that exception and reject it again and leave it compatible hold block and let it spread if you do not have enough information How to recover and / or you do not need to log any message

The exception to promoting the call stack can be more meaningful than returning just a hard-code value such as 10.

For the second question:

Yes, this is a guarantee.

No comments:

Post a Comment