-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptions.py
More file actions
38 lines (34 loc) · 991 Bytes
/
Exceptions.py
File metadata and controls
38 lines (34 loc) · 991 Bytes
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
a = 5
b = 0
try:
print('Result of Division: ', a/b)
except ZeroDivisionError as iE:
print('Cannot be divided by zero')
# We need to see the error first
#print(language)
try:
print(language)
except NameError as e:
print('Some error occurred')
# No Exceptions
language = 'Python'
try:
print(language)
except NameError as e:
print('Some error occurred')
# Syntax: Finally block always executes after normal termination of try block of after try block terminates due to some exception
try:
print(ourVariable)
except Exception:
print('ourVariable is not defined')
finally:
print('Output is printed successfully')
# Exceptions are raised when errors occur at runtime
# We can also choose when the raise exceptions in our code
def print_five_items(data):
if len(data) != 5:
raise ValueError('The argument must have five elements')
for item in data:
print(item)
# Executed
print_five_items([5, 2])