mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Python: add finally and with statements
https://docs.python.org/2/tutorial/errors.html
This commit is contained in:
parent
bbe6d05932
commit
a24e5ef7a1
@ -379,7 +379,13 @@ except (TypeError, NameError):
|
||||
pass # Multiple exceptions can be handled together, if required.
|
||||
else: # Optional clause to the try/except block. Must follow all except blocks
|
||||
print "All good!" # Runs only if the code in try raises no exceptions
|
||||
finally: # Execute under all circumstances
|
||||
print "We can clean up resources here"
|
||||
|
||||
# Instead of try/finally to cleanup resources you can use with
|
||||
with open("myfile.txt") as f:
|
||||
for line in f:
|
||||
print line
|
||||
|
||||
####################################################
|
||||
## 4. Functions
|
||||
|
Loading…
Reference in New Issue
Block a user