Error Handling explained
parent
2322b3951c
commit
b3ca37c38a
|
@ -0,0 +1,243 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Errors and Exceptions:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"OS Error occured\n",
|
||||
"Always going to run\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"try:\n",
|
||||
" with open('newfile.txt','r') as f:\n",
|
||||
" f.write(\"New line to write.\")\n",
|
||||
"except OSError:\n",
|
||||
" print('OS Error occured')\n",
|
||||
"finally:\n",
|
||||
" print('Always going to run')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def ask_for_int():\n",
|
||||
" \n",
|
||||
" while True:\n",
|
||||
" try:\n",
|
||||
" result = int(input(\"Please provide a number: \"))\n",
|
||||
" except:\n",
|
||||
" print(\"\\nIncorrect Value!\\n\")\n",
|
||||
" continue\n",
|
||||
" else:\n",
|
||||
" print(\"\\n Thank you. \")\n",
|
||||
" break\n",
|
||||
" finally:\n",
|
||||
" print(\"End of the code block. \\n\\n\")\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Please provide a number: ty\n",
|
||||
"\n",
|
||||
"Incorrect Value!\n",
|
||||
"\n",
|
||||
"End of the code block. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Please provide a number: 2.3\n",
|
||||
"\n",
|
||||
"Incorrect Value!\n",
|
||||
"\n",
|
||||
"End of the code block. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Please provide a number: 4\n",
|
||||
"\n",
|
||||
" Thank you. \n",
|
||||
"End of the code block. \n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"ask_for_int()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Problem 1\n",
|
||||
"Handle the exception thrown by the code below by using <code>try</code> and <code>except</code> blocks."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Error Occured\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"try:\n",
|
||||
" for i in ['a','b','c']:\n",
|
||||
" print(i**2)\n",
|
||||
"except:\n",
|
||||
" print('Error Occured')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Problem 2\n",
|
||||
"Handle the exception thrown by the code below by using <code>try</code> and <code>except</code> blocks. Then use a <code>finally</code> block to print 'All Done.'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Cannot divide by 0\n",
|
||||
"All Done\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"try:\n",
|
||||
" x = 5\n",
|
||||
" y = 0\n",
|
||||
"\n",
|
||||
" z = x/y\n",
|
||||
"except ZeroDivisionError:\n",
|
||||
" print('Cannot divide by 0')\n",
|
||||
"except:\n",
|
||||
" print('Error Occured')\n",
|
||||
"finally:\n",
|
||||
" print('All Done')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Problem 3\n",
|
||||
"Write a function that asks for an integer and prints the square of it. Use a <code>while</code> loop with a <code>try</code>, <code>except</code>, <code>else</code> block to account for incorrect inputs."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def ask():\n",
|
||||
" \n",
|
||||
" # Waiting for the correct response\n",
|
||||
" waiting = True\n",
|
||||
" while waiting:\n",
|
||||
" try:\n",
|
||||
" number = int(input('Input an integer: '))\n",
|
||||
" except:\n",
|
||||
" print('\\nAn error occurred! Please try again!\\n')\n",
|
||||
" continue\n",
|
||||
" else:\n",
|
||||
" waiting = False\n",
|
||||
" \n",
|
||||
" print(f'\\nThank you, your number squared is {number**2}') "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Input an integer: y\n",
|
||||
"\n",
|
||||
"An error occurred! Please try again!\n",
|
||||
"\n",
|
||||
"Input an integer: r\n",
|
||||
"\n",
|
||||
"An error occurred! Please try again!\n",
|
||||
"\n",
|
||||
"Input an integer: 5\n",
|
||||
"\n",
|
||||
"Thank you, your number squared is 25\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"ask()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
Loading…
Reference in New Issue