| @@ -1,15 +1,12 @@ | |||||
| % Programmation avec Python (chapitre 10) | % Programmation avec Python (chapitre 10) | ||||
| % Dimitri Merejkowsky | % Dimitri Merejkowsky | ||||
| \center \huge Rappels | |||||
| * string formatting (%, .format(), f"") | |||||
| * abstract base classes | |||||
| # | |||||
| \center \huge Exceptions | \center \huge Exceptions | ||||
| * Examples: | |||||
| # Examples d'erreurs | |||||
| * division par zéro | * division par zéro | ||||
| * dépassement d'un tableau | * dépassement d'un tableau | ||||
| * clé non trouvée dans un dico | * clé non trouvée dans un dico | ||||
| @@ -17,9 +14,7 @@ | |||||
| * le fichier n'existe pas | * le fichier n'existe pas | ||||
| * la variable n'existe pas | * la variable n'existe pas | ||||
| * les backtraces | |||||
| * comment les lire | |||||
| * y a un sens! | |||||
| # Les backtraces | |||||
| ```python | ```python | ||||
| def une_fonction(): | def une_fonction(): | ||||
| @@ -31,7 +26,21 @@ def une_autre_fonction(): | |||||
| une_autre_fonction() | une_autre_fonction() | ||||
| ``` | ``` | ||||
| # Les backtraces | |||||
| ``` | |||||
| Traceback (most recent call last): | |||||
| File "foo.py", line 7, in <module> | |||||
| une_autre_fonction() | |||||
| File "foo.py", line 5, in une_autre_fonction | |||||
| une_fonction() | |||||
| File "foo.py", line 2, in une_fonction | |||||
| return 1 / 0 | |||||
| ZeroDivisionError: division by zero | |||||
| ``` | |||||
| # Les backtraces | |||||
| ```python | ```python | ||||
| def une_fonction(diviseur): | def une_fonction(diviseur): | ||||
| @@ -43,8 +52,22 @@ def une_autre_fonction(): | |||||
| une_autre_fonction() | une_autre_fonction() | ||||
| ``` | ``` | ||||
| # Les backtraces | |||||
| ``` | |||||
| Traceback (most recent call last): | |||||
| File "foo.py", line 7, in <module> | |||||
| une_autre_fonction() | |||||
| File "foo.py", line 5, in une_autre_fonction | |||||
| une_fonction(diviseur=0) | |||||
| File "foo.py", line 2, in une_fonction | |||||
| return 1 / diviseur | |||||
| ZeroDivisionError: division by zero | |||||
| ``` | |||||
| # Lever une exception | # Lever une exception | ||||
| ```python | ```python | ||||
| def retirer_somme(compte, montant): | def retirer_somme(compte, montant): | ||||
| solde = ... | solde = ... | ||||
| @@ -54,10 +77,36 @@ def retirer_somme(compte, montant): | |||||
| # Attraper une exception | # Attraper une exception | ||||
| * Quand c'est pas le bon type, ben ça throw quand même | |||||
| * On peut mettre plusieurs blocs de `catch` | |||||
| ```python | |||||
| try: | |||||
| a = 1 / 0 | |||||
| this_will_never_happen() | |||||
| except ZeroDivisionError: | |||||
| print("someone tried to divide by zero!") | |||||
| ``` | |||||
| * Note: si l'exception n'est pas une fille de la classe attrapee, c'est rate. | |||||
| # Attraper une exception | |||||
| * On peut mettre plusieurs blocs de `except` | |||||
| * On peut attraper plusieurs exceptions d'un coup | * On peut attraper plusieurs exceptions d'un coup | ||||
| # Attraper une exception | |||||
| ```python | |||||
| try: | |||||
| something_dangerous() | |||||
| except ZeroDivisionError: | |||||
| print("tried to devide by zero") | |||||
| except FileNotFoundError: | |||||
| print("file not found") | |||||
| ``` | |||||
| Attention aux bare except | Attention aux bare except | ||||
| # Hiérarchies | # Hiérarchies | ||||