0Pricing
Mojo Academy · Lektion

Mit try/except Fehler abfangen

Reagieren Sie elegant auf einen Fehler.

Mit try/except Fehler abfangen ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Catching the Fall

Raising signals a problem, but someone must catch it. Mojo uses a try block to run risky code and recover if it fails. 🪤

The try Block

Code that might fail goes inside try. If it raises, Mojo jumps straight to the matching recovery code below.

try:
    risky_step()

The except Block

The except block holds your recovery plan. It only runs when the try block actually raised an error.

try:
    risky_step()
except:
    print("recovered")

Reading the Error

You can name the caught error to read its message. Here e holds the details of what went wrong.

try:
    risky_step()
except e:
    print("failed:", e)

Normal Path First

If nothing raises, the try block finishes and the except block is skipped entirely. The happy path stays clean.

Recovering Gracefully

Catching lets you show a friendly message or use a fallback value instead of letting the whole program crash. ✨

Keep try Blocks Small

Wrap only the risky line in try, not your whole function. Small blocks make it clear what could fail.

Catching Calls That Raise

Calling a function that may raise requires a try. Here we safely attempt a withdrawal and handle a rejection.

try:
    withdraw(-5)
except e:
    print("denied:", e)

Don't Swallow Silently

An empty except that does nothing hides bugs. At minimum log the error so problems stay visible.

Catch Where You Can Help

Only catch an error where you can actually do something useful about it. Otherwise let it propagate upward.

Try Plus Except as a Pair

Together try and except turn a possible failure into a controlled outcome your program can continue past.

Quick Check

Recall when each block of a try/except actually runs.

Recap

You learned to wrap risky code in try, recover in except, read the error with a name, and keep blocks small and meaningful. 🎯

Häufig gestellte Fragen

Ist die Lektion „Mit try/except Fehler abfangen“ kostenlos?

Ja — der vollständige Text von „Mit try/except Fehler abfangen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Mit try/except Fehler abfangen“?

Reagieren Sie elegant auf einen Fehler. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Mojo Academy zu starten?

Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Mit try/except Fehler abfangen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?

Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einen Fehler auslösen
  2. Mit try/except Fehler abfangen
  3. Die raises-Annotation
  4. Aufräumen mit finally
← Zurück zu Mojo Academy