Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm IDE unreachable code when using try finally within a while loop

Tags:

python

pycharm

The following code causes PyCharm to detect the last line as 'unreachable code', despite it being completely reachable.

import random

def test_func():
    while True:
        print("start")
        try:
            if random.random() > 0.5:
                break
        finally:
            print("Finally")
    print("Done")

if __name__ == '__main__':
    test_func()

When the try/finally block is not included, the error does not occur.

Here's a relevant screenshot:

PyCharm showing print("Done") as unreachable

like image 218
daboross Avatar asked Sep 20 '25 19:09

daboross


1 Answers

The problem was already reported here and here and it should be fixed in PyCharm 2016.3.

like image 148
alecxe Avatar answered Sep 22 '25 09:09

alecxe