1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-12-23 04:08:12 +00:00
sitc/python/1_9_Errors_Exceptions.ipynb

222 lines
5.1 KiB
Plaintext
Raw Normal View History

2016-02-01 12:05:58 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](http://www.upm.es/sfs/Rectorado/Gabinete%20del%20Rector/Logos/UPM/EscPolitecnica/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-03-06 16:46:12 +00:00
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © Carlos A. Iglesias"
2016-02-01 12:05:58 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Errors and Exceptions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Errors\n",
"\n",
"Errors (or syntax errors) are errors because the syntax is incorrect."
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"# Example SyntaxError - missing semicolon in while\n",
"while True\n",
" print \"hi\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exceptions\n",
"Even when the syntax is correct, we can have other types of errors."
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"# Example TypeError - wrong use of '+' with different types\n",
"3 + 'a'"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"# Example NameError: variable not defined\n",
"a \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exceptions can be handled with the statement **try-except-else-finally**. \n",
"\n",
"* **try**: code block when an exception can occur\n",
"* **except**: code block executed if the exception ocurrs (catches the exception)\n",
"* **else**: (optional) code block executed if no exception occurs\n",
"* **finally**: (optional) code block executed always (both if there is an exception or not)"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"# Example\n",
"# Try to enter a wrong input (e.g. a) and then a number (e.g. 0)\n",
"while True:\n",
" try:\n",
" num = int(input(\"Please enter a number: \"))\n",
" break\n",
" except ValueError:\n",
" print(\"Oops! That was no valid number. Try again...\")"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"# Example with finally\n",
"while True:\n",
" try:\n",
" num = int(input(\"Please enter a number: \"))\n",
" break\n",
" except ValueError:\n",
" print(\"Oops! That was no valid number. Try again...\")\n",
" finally:\n",
" print(\"End of input\")"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"# Example with else and finally\n",
"while True:\n",
" try:\n",
" num = int(input(\"Please enter a number: \"))\n",
" except ValueError:\n",
" print(\"Oops! That was no valid number.\")\n",
" continue\n",
" else:\n",
" print(\"No exception\")\n",
" break\n",
" finally:\n",
" print(\"Number entered\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exception can be thrown with 'raise'"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"def add(a, b):\n",
" try:\n",
" return a + b\n",
" except:\n",
" raise TypeError(\"Both arguments should be numbers or strings\")\n",
" \n",
"add(3, 'a')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Licence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
2019-03-06 16:46:12 +00:00
"© Carlos A. Iglesias, Universidad Politécnica de Madrid."
2016-02-01 12:05:58 +00:00
]
}
],
"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",
2019-03-06 16:46:12 +00:00
"version": "3.7.1"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
2016-02-01 12:05:58 +00:00
}
},
"nbformat": 4,
2019-03-06 16:46:12 +00:00
"nbformat_minor": 1
2016-02-01 12:05:58 +00:00
}