lesson 06: try/except error handling

This commit is contained in:
url
2026-07-22 15:08:14 +01:00
parent 9c28ab939b
commit 280e12a702
+20
View File
@@ -0,0 +1,20 @@
#import os
def safe_divide(a, b):
try:
return a / b
except ZeroDivisionError:
return("i can't do that dave")
def safe_read(fn):
try:
f = open(fn)
contents = f.read()
f.close()
return contents
except FileNotFoundError:
return "there is no spoon"
result =safe_divide(100, 10)
print(result)
print(safe_read("errors.py"))
print(safe_read("nonexistent.txt"))