Skip to content

Commit 1b1152e

Browse files
committed
exception
1 parent 1372089 commit 1b1152e

File tree

2 files changed

+753
-30
lines changed

2 files changed

+753
-30
lines changed

exception______________________.ipynb

+341
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "19295e02",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"before\n"
14+
]
15+
},
16+
{
17+
"ename": "NameError",
18+
"evalue": "name 'address' is not defined",
19+
"output_type": "error",
20+
"traceback": [
21+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
22+
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
23+
"Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mbefore\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43maddress\u001b[49m)\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mafter\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
24+
"\u001b[1;31mNameError\u001b[0m: name 'address' is not defined"
25+
]
26+
}
27+
],
28+
"source": [
29+
"print('before')\n",
30+
"print(address)\n",
31+
"print('after')"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 3,
37+
"id": "872f83fd",
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"name": "stdout",
42+
"output_type": "stream",
43+
"text": [
44+
"before\n",
45+
"something went wrong\n",
46+
"after\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"print('before')\n",
52+
"try:\n",
53+
" print(address)\n",
54+
"except:\n",
55+
" print('something went wrong')\n",
56+
"print('after')"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 5,
62+
"id": "3deda1df",
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"name": "stdout",
67+
"output_type": "stream",
68+
"text": [
69+
"division by zero\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"try:\n",
75+
" print(50/0)\n",
76+
"except Exception as err:\n",
77+
" print(err)"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 11,
83+
"id": "c66cbcb4",
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"name": "stdout",
88+
"output_type": "stream",
89+
"text": [
90+
"nishad\n",
91+
"division by zero\n"
92+
]
93+
}
94+
],
95+
"source": [
96+
"try:\n",
97+
" a='nishad'\n",
98+
" print(a)\n",
99+
" print(50/0)\n",
100+
"except IndexError as err:\n",
101+
" print(err)\n",
102+
"except ZeroDivisionError as err:\n",
103+
" print(err)"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": 14,
109+
"id": "362f2231",
110+
"metadata": {},
111+
"outputs": [
112+
{
113+
"name": "stdout",
114+
"output_type": "stream",
115+
"text": [
116+
"name 'rahul' is not defined\n"
117+
]
118+
}
119+
],
120+
"source": [
121+
"try:\n",
122+
" a='nishad'\n",
123+
" print(rahul)\n",
124+
"except(NameError, IndexError, ZeroDivisionError) as err:\n",
125+
" print(err)"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 15,
131+
"id": "18690d61",
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"name": "stdout",
136+
"output_type": "stream",
137+
"text": [
138+
"name 'r' is not defined\n",
139+
"done\n"
140+
]
141+
}
142+
],
143+
"source": [
144+
"try:\n",
145+
" print(r)\n",
146+
"except Exception as err:\n",
147+
" print(err)\n",
148+
"finally: \n",
149+
" print('done')"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 17,
155+
"id": "a7fc4144",
156+
"metadata": {},
157+
"outputs": [
158+
{
159+
"ename": "Exception",
160+
"evalue": "auto raise",
161+
"output_type": "error",
162+
"traceback": [
163+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
164+
"\u001b[1;31mException\u001b[0m Traceback (most recent call last)",
165+
"Input \u001b[1;32mIn [17]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mauto raise\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
166+
"\u001b[1;31mException\u001b[0m: auto raise"
167+
]
168+
}
169+
],
170+
"source": [
171+
"raise Exception('auto raise')"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": 18,
177+
"id": "3b9ee479",
178+
"metadata": {},
179+
"outputs": [
180+
{
181+
"ename": "ValueError",
182+
"evalue": "data not found",
183+
"output_type": "error",
184+
"traceback": [
185+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
186+
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
187+
"Input \u001b[1;32mIn [18]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdata not found\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
188+
"\u001b[1;31mValueError\u001b[0m: data not found"
189+
]
190+
}
191+
],
192+
"source": [
193+
"raise ValueError('data not found')"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 25,
199+
"id": "8033b535",
200+
"metadata": {},
201+
"outputs": [
202+
{
203+
"name": "stdout",
204+
"output_type": "stream",
205+
"text": [
206+
"Enter Your Age: 17.5\n"
207+
]
208+
},
209+
{
210+
"ename": "Exception",
211+
"evalue": "Enter Only Numeric Value",
212+
"output_type": "error",
213+
"traceback": [
214+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
215+
"\u001b[1;31mException\u001b[0m Traceback (most recent call last)",
216+
"Input \u001b[1;32mIn [25]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnot eligible\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mEnter Only Numeric Value\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
217+
"\u001b[1;31mException\u001b[0m: Enter Only Numeric Value"
218+
]
219+
}
220+
],
221+
"source": [
222+
"age=input('Enter Your Age: ')\n",
223+
"if age.isnumeric():\n",
224+
" if int(age)>=18:\n",
225+
" print('eligible')\n",
226+
" else:\n",
227+
" raise Exception ('not eligible')\n",
228+
"else:\n",
229+
" raise Exception('Enter Only Numeric Value')"
230+
]
231+
},
232+
{
233+
"cell_type": "code",
234+
"execution_count": 26,
235+
"id": "e720552c",
236+
"metadata": {},
237+
"outputs": [
238+
{
239+
"ename": "AssertionError",
240+
"evalue": "triggerd",
241+
"output_type": "error",
242+
"traceback": [
243+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
244+
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
245+
"Input \u001b[1;32mIn [26]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtriggerd\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
246+
"\u001b[1;31mAssertionError\u001b[0m: triggerd"
247+
]
248+
}
249+
],
250+
"source": [
251+
"assert False, 'triggerd'"
252+
]
253+
},
254+
{
255+
"cell_type": "code",
256+
"execution_count": 34,
257+
"id": "e3c95332",
258+
"metadata": {},
259+
"outputs": [
260+
{
261+
"name": "stdout",
262+
"output_type": "stream",
263+
"text": [
264+
"Enter Your Age: fg\n"
265+
]
266+
},
267+
{
268+
"ename": "AssertionError",
269+
"evalue": "Enter Only Numerical Value",
270+
"output_type": "error",
271+
"traceback": [
272+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
273+
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
274+
"Input \u001b[1;32mIn [34]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m age\u001b[38;5;241m=\u001b[39m\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mEnter Your Age: \u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m age\u001b[38;5;241m.\u001b[39misnumeric(), \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mEnter Only Numerical Value\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mint\u001b[39m(age)\u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m18\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnot eligible\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124meligible\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
275+
"\u001b[1;31mAssertionError\u001b[0m: Enter Only Numerical Value"
276+
]
277+
}
278+
],
279+
"source": [
280+
"age=input('Enter Your Age: ')\n",
281+
"assert age.isnumeric(), 'Enter Only Numerical Value'\n",
282+
"assert int(age)>=18, 'not eligible'\n",
283+
"print('eligible')"
284+
]
285+
},
286+
{
287+
"cell_type": "code",
288+
"execution_count": 36,
289+
"id": "1f256268",
290+
"metadata": {},
291+
"outputs": [
292+
{
293+
"name": "stdout",
294+
"output_type": "stream",
295+
"text": [
296+
"Enter Your Age: h\n",
297+
"Enter Only Numerical Value\n"
298+
]
299+
}
300+
],
301+
"source": [
302+
"age=input('Enter Your Age: ')\n",
303+
"try:\n",
304+
" assert age.isnumeric(), 'Enter Only Numerical Value'\n",
305+
" assert int(age)>=18, 'not eligible'\n",
306+
" print('eligible')\n",
307+
"except Exception as err:\n",
308+
" print(err)"
309+
]
310+
},
311+
{
312+
"cell_type": "code",
313+
"execution_count": null,
314+
"id": "b2e630f7",
315+
"metadata": {},
316+
"outputs": [],
317+
"source": []
318+
}
319+
],
320+
"metadata": {
321+
"kernelspec": {
322+
"display_name": "Python 3 (ipykernel)",
323+
"language": "python",
324+
"name": "python3"
325+
},
326+
"language_info": {
327+
"codemirror_mode": {
328+
"name": "ipython",
329+
"version": 3
330+
},
331+
"file_extension": ".py",
332+
"mimetype": "text/x-python",
333+
"name": "python",
334+
"nbconvert_exporter": "python",
335+
"pygments_lexer": "ipython3",
336+
"version": "3.9.12"
337+
}
338+
},
339+
"nbformat": 4,
340+
"nbformat_minor": 5
341+
}

0 commit comments

Comments
 (0)