Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should break/continue/return be interrupted by an exception?

I came upon an interesting scenario with flow control while working on my language. What happens if an exception is thrown while processing a break statement. GCC seems to believe the break flow is lost, but the standard seems somewhat silent on what should happen.

For example, what should the following program actually do?

#include <iostream>
using namespace std;

struct maybe_fail {
    bool fail;
    ~maybe_fail() {
        if( fail )
            throw 1;
    }
};

int main() {
    for( int i=0; i < 6; ++i ) {
        cout << "Loop: " << i << endl;

        try {
            maybe_fail mf;
            mf.fail = i % 2;
            if( i == 3 )
                break;

        } catch( int ) {
            cout << "Caught" << endl;
        }
    }
    return 0;
}

Note that a return will also be blocked, as will a continue (add output after the catch to see that). Attempt to goto outside of the block will also be caught.

What is the correct flow? The standard doesn't seem to address this: section 6.6 on jump statements makes no mention, neither does section 15 on exception handling. I do understand that exceptions in destructors is in terribly bad form, but if you are using something like BOOST_SCOPE_EXIT for defer statements this behaviour might become quite important.

Perhaps of interest, the same flow happens in Java and Python, so at least there seems to be some consistency in the imperative languages.

like image 339
edA-qa mort-ora-y Avatar asked Apr 19 '13 04:04

edA-qa mort-ora-y


People also ask

Which of the following is true about break and continue statement?

Answer : They are both jump statements. Explanation : Break statement is used to exit the loop. Continue statement is used to jump to start of next iteration. hence we can say that both statements are used as jump statements.

Should Break statements be avoided?

Overusing break statements is also bad practice because it introduces more exit points. We generally want to keep exit points to a minimum since multiple exit points complicate our logic and make it more difficult to comprehend code.

Is it better to use break or return?

You normally only use break in switch statements, or in loops, and you use return when you want to return to the invoker of a method. Doesn't matter if you are using Android or not.

Can you break out of while loop with return?

To break out of a while loop, you can use the endloop, continue, resume, or return statement.


1 Answers

This is covered in 15.1 Throwing an exception:

2 When an exception is thrown, control is transferred to the nearest handler with a matching type (15.3); “nearest” means the handler for which the compound-statement or ctor-initializer following the try keyword was most recently entered by the thread of control and not yet exited.

Once control is transferred to the exception handler, it just continues from there. There is no mechanism to "remember" that the code was in the middle of a break and then somehow resume that after the exception is handled.

like image 174
NPE Avatar answered Sep 29 '22 00:09

NPE