Breaking out of the middle of a try block in Python

I had some code where I wanted to break out of a try block in Python. The code looked something like this:

    try:
        print "First"
        if some_condition:
            # What do I put here to break out of the try block?
        print "Second"
    except Exception as e:
        if e:
            print "Exception"
    print "Last"

(If some_condition is true, I want the output to print “First” and “Last”)

Neither break or continue works as expected, since “try” isn’t a loop. I could put the body of the try block in a function, and then at the “if some_condition” section, I could just return, but that seemed messy. I never found a clean enough solution, but one possibility is this wonky syntax:

for _ in [True]:
    try:
        print "First"
        if some_condition:
            # Now, since we're in a loop, we can break out of it. 
            break
        print "Second"
    except Exception as e:
        if e:
            print "Exception"
            print type(e)

print "Last"

But, it’s kind of ugly, and not clear what’s going on, so I decided not to do it that way.

I’m really surprised that there isn’t a standard syntax for this. Something like “raise” but that isn’t an exception. I guess I could raise a special value and handle it specially in th except block, but that seems messy as well.

7 thoughts on “Breaking out of the middle of a try block in Python”

  1. Normally, in situations like this I organize the code by means of the conditional within the try block. For example, in your case, you can write:

    if not some_condition:
    print "Second"

    That way, you don’t need to break out of anything.

  2. Agreed 100%, but in my case, there was a lot of stuff after “Second” that I wanted to skip over, so putting that all in an if block was sort of unwieldy.

  3. Personally, I’d probably use your last suggestion—throw a special exception out, and catch it in its own clause of the try statement, e.g.,

    class escape (Exception): pass

    try:
    print(“First”)
    if some_condition:
    raise escape
    print(“Second”)
    except escape:
    pass
    except Exception as e:
    do_something_sensible_with(e)

    I agree, it’s not quite as pretty as having special syntax, but it’s not too bad if there’s a lot going on where you’ve written print(“Second”). Because the escape class is defined in your local scope, and you’re catching it explicitly, you don’t need to worry about any conflict with other exception types you might get from deeper in the call graph.

  4. And how would you skip a part of the code if it wasn’t all enclosed in a try block?


    print "First"
    if some_condition:
    # What do I put here to skip the "Second"?
    print "Second"
    print "Last"

    You’d put “Second” under the if-statement, that’s how. If the skippable part is actually 20 lines and not one, you’d maybe choose to extract it into a function:


    print "First"
    if not some_condition:
    DoSecond()
    print "Last"

    So why wouldn’t the same thing be a clean solution inside a try-block?

Leave a Reply