forked from inspirezonetech/TeachMePythonLikeIm5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handling-exceptions.py
26 lines (21 loc) · 979 Bytes
/
handling-exceptions.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
# ------------------------------------------------------------------------------------
# Tutorial: Handling exceptions in python
# ------------------------------------------------------------------------------------
# You can handle exceptions in a try / except blocks
# The program executes the code in the try block,
# if something goes wrong an exception is raised
# It will be handled by the except block,
# if not the except section will be ignored.
try:
a = 2
a = a - 1
print("Working!!")
if a == 0:
# Here a custom exception is raised if a is equal to 0
raise ValueError("a cannot be 0")
except ValueError:
print('Something went wrong')
raise
# ------------------------------------------------------------------------------------
# Challenge: Create a try / except block and raise a custom exception when the value received is not a number
# ------------------------------------------------------------------------------------