Skip to content

Commit 5363fe6

Browse files
committed
Try error exceptions
1 parent 5a2ca6c commit 5363fe6

File tree

2 files changed

+362
-22
lines changed

2 files changed

+362
-22
lines changed

Diff for: .ipynb_checkpoints/10.Exceptions-checkpoint.ipynb

+323-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,327 @@
11
{
2-
"cells": [],
3-
"metadata": {},
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import sys"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 2,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"x = ['a', 0, 2]"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 3,
24+
"metadata": {},
25+
"outputs": [
26+
{
27+
"data": {
28+
"text/plain": [
29+
"['a', 0, 2]"
30+
]
31+
},
32+
"execution_count": 3,
33+
"metadata": {},
34+
"output_type": "execute_result"
35+
}
36+
],
37+
"source": [
38+
"x"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 4,
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"name": "stdout",
48+
"output_type": "stream",
49+
"text": [
50+
"The entry is a\n",
51+
"Oops! <class 'ValueError'> occured.\n",
52+
"Next entry.\n",
53+
"\n",
54+
"The entry is 0\n",
55+
"Oops! <class 'ZeroDivisionError'> occured.\n",
56+
"Next entry.\n",
57+
"\n",
58+
"The entry is 2\n",
59+
"The reciprocal of 2 is 0.5\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"for entry in x:\n",
65+
" try:\n",
66+
" print(\"The entry is\", entry)\n",
67+
" r = 1/int(entry)\n",
68+
" break\n",
69+
" except:\n",
70+
" print(\"Oops!\",sys.exc_info()[0],\"occured.\")\n",
71+
" print(\"Next entry.\")\n",
72+
" print()\n",
73+
"print(\"The reciprocal of\",entry,\"is\",r)"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 5,
79+
"metadata": {},
80+
"outputs": [
81+
{
82+
"ename": "KeyboardInterrupt",
83+
"evalue": "",
84+
"output_type": "error",
85+
"traceback": [
86+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
87+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
88+
"\u001b[0;32m<ipython-input-5-c761920b81b0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
89+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
90+
]
91+
}
92+
],
93+
"source": [
94+
"raise KeyboardInterrupt"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 6,
100+
"metadata": {},
101+
"outputs": [
102+
{
103+
"ename": "MemoryError",
104+
"evalue": "This is an argument",
105+
"output_type": "error",
106+
"traceback": [
107+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
108+
"\u001b[0;31mMemoryError\u001b[0m Traceback (most recent call last)",
109+
"\u001b[0;32m<ipython-input-6-51782e52f201>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mMemoryError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"This is an argument\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
110+
"\u001b[0;31mMemoryError\u001b[0m: This is an argument"
111+
]
112+
}
113+
],
114+
"source": [
115+
"raise MemoryError(\"This is an argument\")"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 7,
121+
"metadata": {},
122+
"outputs": [
123+
{
124+
"name": "stdout",
125+
"output_type": "stream",
126+
"text": [
127+
"Enter a positive integer: 3.0\n",
128+
"invalid literal for int() with base 10: '3.0'\n"
129+
]
130+
}
131+
],
132+
"source": [
133+
"try:\n",
134+
" a = int(input(\"Enter a positive integer: \"))\n",
135+
" if a <= 0:\n",
136+
" raise ValueError(\"That is not a positive number!\")\n",
137+
"except ValueError as ve:\n",
138+
" print(ve)"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": 8,
144+
"metadata": {},
145+
"outputs": [
146+
{
147+
"name": "stdout",
148+
"output_type": "stream",
149+
"text": [
150+
"Enter a positive integer: 5\n"
151+
]
152+
}
153+
],
154+
"source": [
155+
"try:\n",
156+
" a = int(input(\"Enter a positive integer: \"))\n",
157+
" if a <= 0:\n",
158+
" raise ValueError(\"That is not a positive number!\")\n",
159+
"except ValueError as ve:\n",
160+
" print(ve)"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 9,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": [
169+
" try:\n",
170+
" f = open(\"test.txt\",encoding = 'utf-8')\n",
171+
" # perform file operations\n",
172+
" finally:\n",
173+
" f.close()"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 10,
179+
"metadata": {},
180+
"outputs": [
181+
{
182+
"name": "stdout",
183+
"output_type": "stream",
184+
"text": [
185+
"Second element = 2\n",
186+
"An error occurred\n"
187+
]
188+
}
189+
],
190+
"source": [
191+
"a = [1, 2, 3] \n",
192+
"try:\n",
193+
" print(\"Second element = %d\" %(a[1]) )\n",
194+
" print(\"Fourth element = %d\" %(a[3]) )\n",
195+
"except IndexError: \n",
196+
" print(\"An error occurred\")\n"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": 11,
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"name": "stdout",
206+
"output_type": "stream",
207+
"text": [
208+
"\n",
209+
"Error Occurred and Handled\n"
210+
]
211+
}
212+
],
213+
"source": [
214+
"try :\n",
215+
" a = 3\n",
216+
" if a < 4 : \n",
217+
" b = a/(a-3)\n",
218+
" print(\"Value of b = \", b )\n",
219+
"\n",
220+
"except(ZeroDivisionError, NameError):\n",
221+
" print(\"\\nError Occurred and Handled\")\n"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": 12,
227+
"metadata": {},
228+
"outputs": [],
229+
"source": [
230+
"def funb(a , b): \n",
231+
" try:\n",
232+
" c = ((a+b) / (a-b)) \n",
233+
" except ZeroDivisionError:\n",
234+
" print(\"a/b result in 0\")\n",
235+
" else:\n",
236+
" print(c)\n"
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": 13,
242+
"metadata": {},
243+
"outputs": [
244+
{
245+
"name": "stdout",
246+
"output_type": "stream",
247+
"text": [
248+
"-5.0\n"
249+
]
250+
}
251+
],
252+
"source": [
253+
"funb(2.0, 3.0) "
254+
]
255+
},
256+
{
257+
"cell_type": "code",
258+
"execution_count": 14,
259+
"metadata": {},
260+
"outputs": [
261+
{
262+
"name": "stdout",
263+
"output_type": "stream",
264+
"text": [
265+
"a/b result in 0\n"
266+
]
267+
}
268+
],
269+
"source": [
270+
"funb(3.0, 3.0) "
271+
]
272+
},
273+
{
274+
"cell_type": "code",
275+
"execution_count": 15,
276+
"metadata": {},
277+
"outputs": [
278+
{
279+
"name": "stdout",
280+
"output_type": "stream",
281+
"text": [
282+
"An exception\n"
283+
]
284+
},
285+
{
286+
"ename": "NameError",
287+
"evalue": "Hi there",
288+
"output_type": "error",
289+
"traceback": [
290+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
291+
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
292+
"\u001b[0;32m<ipython-input-15-d3e1cb263964>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNameError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Hi there\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mNameError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"An exception\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mraise\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
293+
"\u001b[0;31mNameError\u001b[0m: Hi there"
294+
]
295+
}
296+
],
297+
"source": [
298+
"try:\n",
299+
" raise NameError(\"Hi there\") \n",
300+
"except NameError:\n",
301+
" print(\"An exception\")\n",
302+
" raise"
303+
]
304+
}
305+
],
306+
"metadata": {
307+
"kernelspec": {
308+
"display_name": "Python 3",
309+
"language": "python",
310+
"name": "python3"
311+
},
312+
"language_info": {
313+
"codemirror_mode": {
314+
"name": "ipython",
315+
"version": 3
316+
},
317+
"file_extension": ".py",
318+
"mimetype": "text/x-python",
319+
"name": "python",
320+
"nbconvert_exporter": "python",
321+
"pygments_lexer": "ipython3",
322+
"version": "3.7.3"
323+
}
324+
},
4325
"nbformat": 4,
5326
"nbformat_minor": 2
6327
}

0 commit comments

Comments
 (0)