{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "[prev: Structuration du code](code-block.ipynb) | [home](../index.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lire les erreurs\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Qu'est-ce c'est ?\n", "\n", "- L'erreur apparait au moment où l'instruction erronée est exécutée dans le cours du programme.\n", "- A priori, l'apparition d'une erreur interrompt le programme immédiatement -> plantage.\n", "- (il est quand même possible de [gérer les exceptions](../more-python/exceptions.ipynb))\n", "\n", "\n", "## Les informations fournies par une erreur\n", "\n", "- le type d'erreur\n", "- le message d'erreur\n", "- la ligne de code en jeu\n", "- la pile d'appel -> *traceback*\n", "\n", "Coder == Débugger, autant utiliser l'information qu'on nous donne." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exemples classiques d'erreurs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### NameError" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "NameError", "evalue": "name 'nexistepas' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mnexistepas\u001b[49m\n", "\u001b[1;31mNameError\u001b[0m: name 'nexistepas' is not defined" ] } ], "source": [ "nexistepas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ZeroDivisionError" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;241;43m2\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\n", "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "2 / 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### TypeError" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"int\") to str", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mun\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\n", "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ "'un' + 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### IndexError" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "IndexError", "evalue": "string index out of range", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[4], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m hey \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mbonjour\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mhey\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m9\u001b[39;49m\u001b[43m]\u001b[49m\n", "\u001b[1;31mIndexError\u001b[0m: string index out of range" ] } ], "source": [ "hey = 'bonjour'\n", "hey[9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### KeyError" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "KeyError", "evalue": "'deux'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[5], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m foobar\u001b[38;5;241m=\u001b[39m{\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mun\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1\u001b[39m}\n\u001b[1;32m----> 2\u001b[0m \u001b[43mfoobar\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mdeux\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\n", "\u001b[1;31mKeyError\u001b[0m: 'deux'" ] } ], "source": [ "foobar={'un': 1}\n", "foobar['deux']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## *Traceback* et imbrication d'appels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "La *Traceback* permet de suivre l'imbrication des appels au moment de l'erreur." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[6], line 13\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mf3\u001b[39m(x):\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m f2(x) \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m3\u001b[39m\n\u001b[1;32m---> 13\u001b[0m \u001b[43mf3\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", "Cell \u001b[1;32mIn[6], line 11\u001b[0m, in \u001b[0;36mf3\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mf3\u001b[39m(x):\n\u001b[1;32m---> 11\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf2\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m3\u001b[39m\n", "Cell \u001b[1;32mIn[6], line 8\u001b[0m, in \u001b[0;36mf2\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mf2\u001b[39m(x):\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf1\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m2\u001b[39m\n", "Cell \u001b[1;32mIn[6], line 5\u001b[0m, in \u001b[0;36mf1\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mf1\u001b[39m(x):\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf0\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m1\u001b[39m\n", "Cell \u001b[1;32mIn[6], line 2\u001b[0m, in \u001b[0;36mf0\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mf0\u001b[39m(x):\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\n", "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "def f0(x):\n", " return x / 0\n", "\n", "def f1(x):\n", " return f0(x) / 1\n", "\n", "def f2(x):\n", " return f1(x) / 2\n", "\n", "def f3(x):\n", " return f2(x) / 3\n", "\n", "f3(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercices" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }