python: try/except/else and continue statement
python, python-2.7
Solution
The tutorial gives a good start, but is not the language reference. Read the reference here.
Note in particular:
The optional else clause is executed if and when control flows off the end of the try clause.
clarified by footnote 2:
Currently, control “flows off the end” except in the case of an exception or the execution of a return, continue, or break statement.
So your use of `continue` is explicitly addressed by that.
Problem
Why is the output of the below python code snippet NOT just No exception:1, since during first iteration there is no exception raised. From python docs (https://docs.python.org/2.7/tutorial/errors.html). The try ... except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception. ``` $ cat hello.py for x in range(1,10): try: if x == 1: continue x/0 except Exception: print "Kaput:%s" %(x) else: print "No exception:%s" %(x) break $ python hello.py Kaput:2 Kaput:3 Kaput:4 Kaput:5 Kaput:6 Kaput:7 Kaput:8 Kaput:9 $ python -V Python 2.7.8 ```