Skip to content
BENIBIX Learn

Exception

Development🧩 Programming ConceptsErrors

Also known as Exception Handling, Try Catch, try/except

A signal that something broke mid-run. Code can catch it so the app does not crash.

Think of it like

Think of it like a net under a high rope. If the walker slips, the net catches them.

Example

A sign-up form tries int(age). When Jack types twelve, the form catches the ValueError and asks for a number.

Why it matters

Real input is messy and networks fail. Catching exceptions keeps one bad case from taking the whole app down.

Definition

An exception is a signal a program sends when something fails mid-run. If nothing catches it, the program stops. Code can wrap risky lines in a try block and catch the exception in an except or catch block. There it can show a kind message, try again or use a default. Exceptions let a program fail gently instead of crashing.

Example code

python
try:
    age = int("twelve")
except ValueError:
    print("Please type a number")
# prints: Please type a number
The bad input is caught, so the program asks again instead of crashing.

Common mistake

Catching every exception and doing nothing. The bug is hidden, not fixed, and it comes back later.

Key takeaways

  1. 1
    Raised on trouble. Something failed while the code ran.
  2. 2
    Catch what you expect. Name the kind of error you can handle.
  3. 3
    Never hide it. Log it or tell the user.

Learn it properly