-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathP9_ExceptionHandling.py
More file actions
66 lines (42 loc) · 924 Bytes
/
P9_ExceptionHandling.py
File metadata and controls
66 lines (42 loc) · 924 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
EXCEPTION HANDLING
In [1]:
try:
print(x)
except:
print("An exception occurred")
An exception occurred
In [4]:
a=5
b=0
try:
print(a/b)
except:
print("A number is not divisible by zero")
A number is not divisible by zero
In [12]:
# Program to depict else clause with try-except
# Function which returns a/b
def AbyB(a , b):
try:
c = ((a+b) / (a-b))
except ZeroDivisionError:
print("a/b result in 0")
else:
print(c)
# Driver program to test above function
AbyB(2.0, 3.0)
AbyB(3.0, 3.0)
-5.0
a/b result in 0
In [14]:
# Python program to handle simple runtime error
a = [1, 2, 3]
try:
print ("Second element = %d" %(a[1]))
# Throws error since there are only 3 elements in array
print ("Fourth element = %d" %(a[3]))
except IndexError:
print ("An error occurred")
Second element = 2
An error occurred