Gestion des exceptions en Python
Hierarchie des exceptions
- La classe Exception regroupe (presque) toutes les exceptions Python
- Exceptions génériques : ArithmeticError, LookupError, TypeError, ValueError
- Consulter la hierarchie complète des exceptions
Exceptions courantes
- ValueError : valeur invalide
- TypeError : type invalide
- ZeroDivisionError : division par zéro
- IndexError / KeyError : index/clé inexistant (liste/dictionnaire)
Exceptions systèmes
- OSError : déclanchée par les fonctions os.*()
- IOError : erreur lors de l'accès à un fichier ou un dossier
- EOFError : fin du fichier lu
Exceptions Python
- NameError : variable non définie
- AttributeError : attribut non défini
- SyntaxError : syntaxe invalide
Exceptions spéciales
- KeyboardInterrupt : signaux SIGINT et SIGTERM (CTRL+C)
- SystemExit : fonction sys.exit()
- StopIteration : fin d'un générateur
- MemoryError : Toute la mémoire allouable est utilisée
Changements Python 2.5
- KeyboardInterrupt n'hérite plus de StandardError
- KeyboardInterrupt et SystemExit n'héritent plus de Exception mais de BaseException