17. Lektion: Fehlerbehandlung: Difference between revisions
Jump to navigation
Jump to search
| Line 20: | Line 20: | ||
== try/except== | == try/except== | ||
Dieses ist die klassische Fehlerbehandlung in Python. | |||
=== Fehlercode für Ausnahmen (Exceptions)=== | |||
Es gibt eine ganze Menge Fehlermeldungen die in Python enthalten sind und mit exception ausgewertet werden können: | |||
* AssertionError | |||
* AttributeError | |||
* Exception | |||
* ImportError | |||
* IndexError | |||
* KeyboardInterrupt | |||
* KeyError | |||
* MemoryError | |||
* NameError | |||
* NotImplementedError | |||
* OSError | |||
* RuntimeError | |||
* StopIteration | |||
* SyntaxError | |||
* SystemExit | |||
* TypeError | |||
* ValueError | |||
* ZeroDivisionError | |||
Revision as of 13:20, 3 October 2023
assert
Assert prüft eine Bedingung und gibt wenn False eine Meldung aus.
Das Programm wird dadurch abgebrochen.
>>> a = 1
>>> b = 2
>>> assert a < b, 'a ist kleiner als b'
>>>
>>> assert not a < b, 'a ist kleiner als b'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: a ist kleiner als b
>>>
Übung:
- Teste verschiedene Möglichkeiten um eine assert-Meldung auszulösen.
try/except
Dieses ist die klassische Fehlerbehandlung in Python.
Fehlercode für Ausnahmen (Exceptions)
Es gibt eine ganze Menge Fehlermeldungen die in Python enthalten sind und mit exception ausgewertet werden können:
- AssertionError
- AttributeError
- Exception
- ImportError
- IndexError
- KeyboardInterrupt
- KeyError
- MemoryError
- NameError
- NotImplementedError
- OSError
- RuntimeError
- StopIteration
- SyntaxError
- SystemExit
- TypeError
- ValueError
- ZeroDivisionError
defensive-programming-in-phyton
https://realpython.com/python-catch-multiple-exceptions/
Zurück zu Micropython Kurs 2023 - Teil 1
Zurück zur "Micropython Kurs 2023" Startseite
Zurück zur Programmieren Startseite
Zurück zur Wiki Startseite