-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_handling.py
38 lines (29 loc) · 880 Bytes
/
error_handling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# TRY EXCEPT
def proclaim_user_birthday(name, age):
try:
new_age = age + 1
message = f"{name} is now {new_age} years old!"
print(message)
except Exception as e:
print(e)
print("ONLY WHEN MY PROGRAM CONTINUES")
proclaim_user_birthday("Drew", "45")
print("I'm after proclaim.")
# Anytime you're deailing with user-input data, use error handling.
# When dealing with data that only we control, might be safe enough to disregard error handling.
def find_user(user_id):
if not isinstance(user_id, int):
try:
user_id = int(user_id)
except Exception as e:
print(e)
raise TypeError("User_id must be a number.")
return "A user"
# Rest of code block here
find_user("test")
"""
def example_fn(PARAMETER):
code block here
code block here
example_fun(ARGUMENT)
"""