{ "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, © 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": {}, "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": {}, "outputs": [], "source": [ "# Example TypeError - wrong use of '+' with different types\n", "3 + 'a'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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": {}, "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": {}, "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": {}, "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": {}, "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", "© 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.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 } }, "nbformat": 4, "nbformat_minor": 1 }