lesson 06: try/except error handling
This commit is contained in:
@@ -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"))
|
||||
Reference in New Issue
Block a user