Apr-28-2019, 08:45 PM
Hi all,
I've been trying to tighten up my code by adding error handling using the try command, but I can't seem to get my head around the "finally" statement. I admit I have just been using Python with pygame to make games but I can't seem to find an occassion where I need to use the "finally" statement in a "try" block. I can only presume that I don't really understand it.
Does anyone have any scenarios where its proven more convenient to include the "finally" statement block?
Looking at the common example of cleaning up and not leaving a file open, I don't understand
why I just don't just have the close statement without the "finally" block? Was it just introduced to act as a sort of self documentation?
I've been trying to tighten up my code by adding error handling using the try command, but I can't seem to get my head around the "finally" statement. I admit I have just been using Python with pygame to make games but I can't seem to find an occassion where I need to use the "finally" statement in a "try" block. I can only presume that I don't really understand it.
Does anyone have any scenarios where its proven more convenient to include the "finally" statement block?

""" A module to try and work out the 'finally' statement in a 'try' block. """ try: f = open("demofile.txt") f.write("Lorum Ipsum") except: print("Something went wrong when writing to the file.") finally: f.close()PS. The above code requires the text file to be read-only.
Looking at the common example of cleaning up and not leaving a file open, I don't understand
