17. Lektion: Fehlerbehandlung: Difference between revisions
| (4 intermediate revisions by the same user not shown) | |||
| Line 20: | Line 20: | ||
== try/except== | == try/except== | ||
Dieses ist die klassische Fehlerbehandlung in Python. (Beispiel für Python) | Dieses ist die klassische Fehlerbehandlung in Python. (Beispiel für Python)<br> | ||
Hier liegt in der Hand des Programmierers, ob das Programm abgebrochen, oder weitergeführt wird.<br> | |||
Unter except kann ggf. eine Workaround eingebaut werden. | |||
<pre> | <pre> | ||
| Line 65: | Line 67: | ||
=== Sparversion=== | === Sparversion=== | ||
* https://python.plainenglish.io/python-up-your-code-exception-handling-d57f8fded8be | |||
Ggf. funktioniert das auch ohne Exception Fehlerauswertung: | Ggf. funktioniert das auch ohne Exception Fehlerauswertung: | ||
<pre> | <pre> | ||
| Line 77: | Line 82: | ||
Diese Version ist nicht gerade pythonisch, aber bei der Entwicklung sehr hilfreich. | Diese Version ist nicht gerade pythonisch, aber bei der Entwicklung sehr hilfreich. | ||
== | == Weiterführende Links:== | ||
https://www.python-kurs.eu/python3_ausnahmebehandlung.php | * https://www.python-kurs.eu/python3_ausnahmebehandlung.php | ||
https://phyton.plainenglish.io/defensive-programming-in-phyton-af0266e65dfd | * https://phyton.plainenglish.io/defensive-programming-in-phyton-af0266e65dfd | ||
https://realpython.com/python-catch-multiple-exceptions/ | * https://realpython.com/python-catch-multiple-exceptions/ | ||
* https://medium.com/@noransaber685/python-error-handling-understanding-syntax-errors-exceptions-and-exception-handling-f3c3109335da | |||
== Navigation== | == Navigation== | ||
Latest revision as of 11:30, 4 November 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. (Beispiel für Python)
Hier liegt in der Hand des Programmierers, ob das Programm abgebrochen, oder weitergeführt wird.
Unter except kann ggf. eine Workaround eingebaut werden.
try:
# Diese Anweisung kann einen FileNotFoundError auslösen:
file = open('/tmp/any_file.txt')
except FileNotFoundError:
print("Datei nicht gefunden!")
except IOError:
print("Datei nicht lesbar!")
else:
# Datei einlesen, wenn kein Fehler augetreten ist:
data = file.read()
finally:
# Diese Anweisung in jedem Fall ausführen:
file.close()
Micropython Fehlercode (Exceptions)
Es gibt eine ganze Menge Fehlermeldungen die in Python enthalten sind und mit exception ausgewertet werden können. In Micropython sind es weniger:
- AssertionError
- AttributeError
- Exception
- ImportError
- IndexError
- KeyboardInterrupt
- KeyError
- MemoryError
- NameError
- NotImplementedError
- OSError
- RuntimeError
- StopIteration
- SyntaxError
- SystemExit
- TypeError
- ValueError
- ZeroDivisionError
Weitere Exceptions konnen durch Module dazu kommen.
Sparversion
Ggf. funktioniert das auch ohne Exception Fehlerauswertung:
>>> try:
1/0
except:
print("Geht nicht!")
Geht nicht!
>>>
Diese Version ist nicht gerade pythonisch, aber bei der Entwicklung sehr hilfreich.
Weiterführende Links:
- https://www.python-kurs.eu/python3_ausnahmebehandlung.php
- https://phyton.plainenglish.io/defensive-programming-in-phyton-af0266e65dfd
- https://realpython.com/python-catch-multiple-exceptions/
- https://medium.com/@noransaber685/python-error-handling-understanding-syntax-errors-exceptions-and-exception-handling-f3c3109335da
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