I am just asking a general quesion which arised out of curiosity when I was working on one of the library project where in another method throws exception insead of return value but I want to convert that exception to return value. I solved it using multiple try catch blocks but just want to know is there a way in C++ to resume after an exception has been thrown.
I'm not completely sure what you want. Converting an exception into a return code is not resuming after the exception. The C++ committee considered resuming, but it wasn't clear what that might mean. Consider:
int
someFunction()
{
// ...
if ( someCondition ) {
throw SomeException();
}
assert( !someCondition );
// ...
}
In C++, the assert can never trigger (and one wouldn't
normally write it, since the if and the throw make it clear
that the asserted condition is valid afterwards). If you could
resume the exception, the assert would (or at least might)
trigger; you'd loose an important means of code validation.
Also people who had used it in other languages reported that it didn't actually work well, and that in every case where they'd started using resumption, they'd had to change the code to not use it later.
No. In fact, the compiler may not even emit instructions for unreachable code after an exception is thrown:
throw std::invalid_argument;
int i = 5; // No instructions generated for this, as it is unreachable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With