[Introduction à l'application Udemy Python3 +] 65. Gestion des exceptions

** * Cet article provient d'Udemy "[Introduction à Python3 enseignée par des ingénieurs actifs de la Silicon Valley + application + style de code de style américain de la Silicon Valley](https://www.udemy.com/course/python-beginner/" Introduction à Python3 enseignée par des ingénieurs actifs de la Silicon Valley + application + Style de code de style de la Silicon Valley américaine ")" C'est une note de classe pour moi après avoir suivi le cours. Il est ouvert au public avec la permission de l'instructeur Jun Sakai. ** **

■ Traitement des exceptions

◆ Basique

l = [0, 1, 2]
i = 5

try:
    print(l[i])
except:
    print('Error!')

result


Error!

Si vous essayez de faire ce que vous écrivez dans essayez: et que cela échoue en raison d'une erreur Le traitement dans ʻexcept: `est exécuté.

◆ Spécification du type d'erreur

l = [0, 1, 2]
del l

try:
    print(l)
except IndexError as ex:
    print('Error massage: {}'.format(ex))
except NameError as ex:
    print('Error massage: {}'.format(ex))

result


Error massage: name 'l' is not defined

Si c'est ʻIndexError, ce processus, Vous pouvez également spécifier le type d'erreur, tel que ce processus s'il s'agit de NameError`.

◆finally:

l = [0, 1, 2]
i = 5

try:
    print(l[i])
except:
    print('Error!')
finally:
    print('clean up')

result


Error!
clean up

Le processus écrit dans finally: est toujours exécuté indépendamment du fait que try réussisse ou sauf.

l = [0, 1, 2]
i = 5

try:
    print(l[i])
finally:
    print('clean up')

result


clean up
Traceback (most recent call last):
  File "/~~~", line 5, in <module>
    print(l[i])
IndexError: list index out of range

Par conséquent, même si une erreur se produit réellement, le traitement dans `enfin 'est exécuté en premier, puis le programme s'arrête avec une erreur.

◆else:

l = [0, 1, 2]

try:
    print(l[0])
except:
    print('Error!')
else:
    print('done')

result


0
done

Avec ʻelse: , le traitement dans ʻelse: n'est exécuté que si le traitement dans try: réussit.

Recommended Posts

[Introduction à l'application Udemy Python3 +] 65. Gestion des exceptions
[Présentation de l'application Udemy Python3 +] 58. Lambda
[Présentation de l'application Udemy Python3 +] 31. Commentaire
[Présentation de l'application Udemy Python3 +] 57. Décorateur
[Présentation de l'application Udemy Python3 +] 56. Clôture
[Présentation de l'application Udemy Python3 +] 59. Générateur
[Introduction à l'application Udemy Python3 +] Résumé
[Introduction à Udemy Python3 + Application] 18. Méthode List
[Introduction à Udemy Python3 + Application] 63. Notation d'inclusion du générateur
[Introduction à Udemy Python3 + Application] 25. Méthode de type dictionnaire
[Introduction à l'application Udemy Python3 +] 33. instruction if
[Introduction à Udemy Python3 + Application] 13. Méthode de caractères
[Introduction à l'application Udemy Python3 +] 55. Fonctions intégrées
[Introduction à l'application Udemy Python3 +] 48. Définition des fonctions
[Introduction à l'application Udemy Python3 +] 10. Valeur numérique
[Introduction à l'application Udemy Python3 +] 41. fonction d'entrée
[Introduction à l'application Udemy Python3 +] 17. Opération de liste
[Introduction à l'application Udemy Python3 +] 11. Chaîne de caractères
[Introduction à l'application Udemy Python3 +] 44. fonction range
[Introduction à l'application Udemy Python3 +] 46. fonction zip
[Introduction à l'application Udemy Python3 +] 24. Type de dictionnaire
[Introduction à Udemy Python3 + Application] 8. Déclaration de variable
[Introduction à Udemy Python3 + Application] 29. Méthode Set
[Introduction à l'application Udemy Python3 +] 16. Type de liste
[Introduction à Udemy Python3 + Application] 61. Notation d'inclusion de dictionnaire
[Introduction à l'application Udemy Python3 +] 22. Déballage de taples
[Introduction à Udemy Python3 + Application] 26. Copie du dictionnaire
[Introduction à l'application Udemy Python3 +] 23. Comment utiliser Tapuru
[Introduction à Udemy Python3 + Application] 60. Notation d'inclusion de liste
[Introduction à Udemy Python3 + Application] 19. Copie de la liste
[Introduction à Udemy Python3 + Application] 38. Lors du jugement Aucun
[Introduction à l'application Udemy Python3 +] 40. Instruction while else
[Introduction à Udemy Python3 + Application] 62. Définir la notation d'inclusion
[Introduction à l'application Udemy Python3 +] 64. Espace de noms et portée
[Introduction à l'application Udemy Python3 +] 43. instruction for else
[Introduction à Udemy Python3 + Application] 67. Arguments de ligne de commande
[Introduction à l'application Udemy Python3 +] 9. Tout d'abord, imprimez avec print
[Introduction à l'application Udemy Python3 +] 54. Qu'est-ce que Docstrings?
[Introduction à Udemy Python3 + Application] 14. Substitution de caractères 15.f-strings
[Introduction à l'application Udemy Python3 +] 35. Opérateurs de comparaison et opérateurs logiques
[Introduction à l'application Udemy Python3 +] 66. Création de votre propre exception
[Introduction à Udemy Python3 + Application] 53. Dictionnaire des arguments de mots-clés
[Introduction à Udemy Python3 + Application] 27. Comment utiliser le dictionnaire
[Introduction à Udemy Python3 + Application] 30. Comment utiliser l'ensemble
[Introduction à Udemy Python3 + Application] 68. Instruction d'importation et AS
[Introduction à Udemy Python3 + Application] 52. Tapple d'arguments positionnels
Gestion des exceptions Python
Gestion des exceptions Python
[Introduction à l'application Udemy Python3 +] 42. pour instruction, instruction break et instruction continue
[Introduction à l'application Udemy Python3 +] 39. instruction while, instruction continue et instruction break
[Introduction à l'application Udemy Python3 +] 36. Utilisation de In et Not
[Introduction à l'application Udemy Python3 +] 32.1 Lorsqu'une ligne devient longue
[Introduction à Udemy Python3 + Application] 50. Arguments de position, arguments de mots-clés et arguments par défaut
[Introduction à Udemy Python3 + Application] 51. Soyez prudent avec les arguments par défaut
J'ai essayé de résumer la gestion des exceptions Python
[Introduction à cx_Oracle] (12e) Gestion des exceptions DB
[Introduction à Udemy Python3 + Application] 49. Citation de fonction et déclaration de valeur de retour
[Introduction à Udemy Python3 + Application] 69. Importation du chemin absolu et du chemin relatif
[Introduction à l'application Udemy Python3 +] 12. Indexation et découpage des chaînes de caractères
Introduction à OpenCV (python) - (2)
Python, à propos de la gestion des exceptions