This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Document weird behavior of `finally` when it has `break` in it
Type: enhancement Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: cool-RR, docs@python, josh.r
Priority: normal Keywords:

Created on 2015-12-10 14:08 by cool-RR, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg256173 - (view) Author: Ram Rachum (cool-RR) * Date: 2015-12-10 14:08
Today I spent 30 minutes because of weird behavior of `finally` when it has a `break` statement inside of it. (The behavior is that the exception gets suppressed, which I found unexpected.) I think this behavior should be documented.

Example:

    
    
    c:\Users\Administrator\Desktop>cat s.py                    
    for x in [1, 2, 3]:                                        
      try:                                                     
        1/0                                                    
      finally:                                                 
        break                                                  
    print(x)                                                   
                                                              
    c:\Users\Administrator\Desktop>c:\Python34\python.exe s.py 
    1
msg256174 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2015-12-10 14:59
You used explicit control flow keywords and you're surprised that it listened?

Regardless, this is already documented https://docs.python.org/3/reference/compound_stmts.html#finally :

"If the finally clause executes a return or break statement, the saved exception is discarded"
History
Date User Action Args
2022-04-11 14:58:24adminsetgithub: 70019
2015-12-10 15:06:11r.david.murraysetstatus: open -> closed
resolution: not a bug
stage: resolved
2015-12-10 14:59:33josh.rsetnosy: + josh.r
messages: + msg256174
2015-12-10 14:08:20cool-RRcreate