-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chapter 30 - ExceptionHandling.py
42 lines (33 loc) · 1.14 KB
/
Chapter 30 - ExceptionHandling.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
39
40
41
42
# CHAPTER 30
# exception = events detected during execution that interrupts the flow of a program
# The code below can result to an exception
# since when you provided 0 to denominator, the exception
# will be caught which is ZeroDivisionError
# numerator = int(input("Enter a number to divide: "))
# denominator = int(input("Enter a number to divide by: "))
# result = numerator / denominator
# print(result)
# To solve this problem, we use exception handling
try:
numerator = int(input("Enter a number to divide: "))
denominator = int(input("Enter a number to divide by: "))
result = numerator / denominator
# Handles the exception ZeroDivisionError only
except ZeroDivisionError as e:
print(e)
print("You can't divide by zero! Idiot!")
# Handles the exception ValueError
except ValueError as e:
print(e)
print("Enter only number plz!")
# Catches all exception
except Exception as e:
print(e)
print("something went wrong")
# If there are no exceptions, print the result
else:
print(result)
# Whether or not there is exception
# this line of code will be always executed
finally:
print("This will always execute")