1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-12 18:02:28 +00:00
sitc/python/1_9_Errors_Exceptions.ipynb
2016-02-15 09:01:37 +01:00

220 lines
4.9 KiB
Plaintext

{
"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": [
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias"
]
},
{
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Example TypeError - wrong use of '+' with different types\n",
"3 + 'a'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"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",
"© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
]
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}