-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment13.py
More file actions
113 lines (93 loc) · 2.18 KB
/
Assignment13.py
File metadata and controls
113 lines (93 loc) · 2.18 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#(Q.1)- Name and handle the exception occured in the following program:
#a=3
#if a<4:
# a=a/(a-3)
# print(a)
try:
a = 3
if a<4:
a = a/(a-3)
print(a)
except Exception as e:
print("Exception Occur")
print(a)
print(e)
#(Q.2)- Name and handle the exception occurred in the following program:
#l=[1,2,3]
#print(l[3])
l = [1,2,3]
try:
print(l[3])
except Exception as index:
print(index)
#(Q.3)- What will be the output of the following code:
# Program to depict Raising Exception
# try:
# raise NameError("Hi there") # Raise Error
# except NameError:
# print "An exception"
# raise # To determine whether the exception was raised or not
try:
raise NameError("Hi there")
except NameError as message:
print(message)
print("An exception")
# OUTPUT= Hi there
# An exception
#(Q.4)- What will be the output of the following code:
# 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)
def AbyB(a,b):
try:
c = (a+b)/(a-b)
except ZeroDivisionError as e:
print(e)
print("a/b result in 0")
else:
print(c)
AbyB(2.0,3.0)
AbyB(3.0,3.0)
#OUTPUT= -5.0
# float division by zero
# a/b result in 0
#(Q.5)- Write a program to show and handle following exceptions:
# 1. Import Error
# 2. Value Error
# 3. Index Error
try:
import abcd
except ImportError as e:
print(e)
try:
a = 4
b = 'v'
c = a+b
print(c)
except Exception as f:
print(f)
try:
l = [2,4,7]
print(l[4])
except IndexError as g:
print(g)
#(Q.6)- Create a user-defined exception AgeTooSmallError() that warns the user when they have entered age less than 18.
# The code must keep taking input till the user enters the appropriate age number(less than 18).
class AgeTooSmallError(Exception):
pass
try:
i = int(input("Enter any age: "))
if i < 18:
raise AgeTooSmallError("This value is too small from 18")
except AgeTooSmallError as e:
print(e)
else:
print("this value is too large from 18")