Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resumable exceptions

Tags:

c++

visual-c++

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.

like image 523
anonymous Avatar asked Mar 20 '26 20:03

anonymous


2 Answers

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.

like image 100
James Kanze Avatar answered Mar 22 '26 14:03

James Kanze


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.
like image 21
MSalters Avatar answered Mar 22 '26 13:03

MSalters