Skip to content

Commit c8b5497

Browse files
authored
Add files via upload
1 parent d6985a2 commit c8b5497

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

Error Handling Udemy.ipynb

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#try, except and finally blocks"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 12,
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"Add went well!\n",
22+
"20\n"
23+
]
24+
}
25+
],
26+
"source": [
27+
"try:\n",
28+
" #WANT TO ATTEMPT THIS CODE\n",
29+
" #MAY HAVE ERROR\n",
30+
" result= 10+10 #Change second integer to string to get to except block\n",
31+
"except:\n",
32+
" print(\"Hey it looks like you aren't adding correcty!\")\n",
33+
"else:\n",
34+
" print(\"Add went well!\")\n",
35+
" print(result)"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": []
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": []
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": []
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": []
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 15,
69+
"metadata": {},
70+
"outputs": [
71+
{
72+
"name": "stdout",
73+
"output_type": "stream",
74+
"text": [
75+
"I always run\n"
76+
]
77+
}
78+
],
79+
"source": [
80+
"try:\n",
81+
" f=open('testfile', 'w') #Change to 'r' to replicate an OSError\n",
82+
" f.write(\"Write a test line\")\n",
83+
"except TypeError:\n",
84+
" print(\"There was a type error\")\n",
85+
"except OSError: #In case there is no access to the file\n",
86+
" print(\"Hey you have an OS error\")\n",
87+
"finally:\n",
88+
" print(\"I always run\")\n",
89+
" \n",
90+
" "
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": []
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": null,
103+
"metadata": {},
104+
"outputs": [],
105+
"source": []
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": []
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 24,
117+
"metadata": {},
118+
"outputs": [],
119+
"source": [
120+
"def ask_for_int():\n",
121+
" while True:\n",
122+
" try:\n",
123+
" result= int(input(\"Please enter a Number: \"))\n",
124+
" except:\n",
125+
" print(\"Whoops! This is not a number\")\n",
126+
" continue\n",
127+
" else:\n",
128+
" print(\"Yes, thank you\")\n",
129+
" break\n",
130+
" finally:\n",
131+
" print(\"End of try/except/finally\")\n",
132+
" #print(result)\n",
133+
" "
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {},
140+
"outputs": [
141+
{
142+
"name": "stdout",
143+
"output_type": "stream",
144+
"text": [
145+
"Please enter a Number: j\n",
146+
"Whoops! This is not a number\n",
147+
"End of try/except/finally\n"
148+
]
149+
}
150+
],
151+
"source": [
152+
"ask_for_int()"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"metadata": {},
159+
"outputs": [],
160+
"source": []
161+
}
162+
],
163+
"metadata": {
164+
"kernelspec": {
165+
"display_name": "Python 3",
166+
"language": "python",
167+
"name": "python3"
168+
},
169+
"language_info": {
170+
"codemirror_mode": {
171+
"name": "ipython",
172+
"version": 3
173+
},
174+
"file_extension": ".py",
175+
"mimetype": "text/x-python",
176+
"name": "python",
177+
"nbconvert_exporter": "python",
178+
"pygments_lexer": "ipython3",
179+
"version": "3.8.5"
180+
}
181+
},
182+
"nbformat": 4,
183+
"nbformat_minor": 4
184+
}

0 commit comments

Comments
 (0)