Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 06

Forms of except Blocks — Multi-Exception, Alias, and Default

Learn all six forms of except blocks in Python — single handlers, tuple grouping, alias, bare except, and enterprise Exception fallback patterns.

Intermediate40 min
Forms of except blocks hero Module 15 Lesson 6 MahiDev

Different errors need different messages. Python provides six forms of except so you can handle each case precisely.

What you will learn

  • Single exception handlers
  • Multiple exception handlers
  • Tuple exception grouping
  • Exception alias (as err)
  • Generic exception handling
  • Best practices used in real-world projects

Why are there different forms of except?

Imagine you are building a banking application. Different errors need different user messages.

Error User message
Wrong PIN "Invalid PIN"
No balance "Insufficient balance"
Server error "Please try again later"

Showing the same message for every error creates confusion. That is why Python provides multiple except forms.

Real-life visualization

Banking user action exception except friendly message MahiDev

Form 1 — One except per exception type

This is the most common and recommended form.

Form 1 try block ValueError ZeroDivisionError separate handlers

Code

python
try:
    a = int("abc")
    b = 10 / 0

except ValueError:
    print("Please enter numbers only")

except ZeroDivisionError:
    print("Cannot divide by zero")

What happens?

  1. int("abc") raises ValueError
  2. First matching except executes
  3. Remaining except blocks are skipped

Output:

text
Please enter numbers only

Form 2 — Tuple exception handler

Sometimes multiple exceptions require the same response.

Instead of writing many blocks:

python
except ValueError:
    ...
except TypeError:
    ...

You can group them:

python
try:
    value = int("abc")

except (ValueError, TypeError):
    print("Input problem")

Visual representation

Form 2 tuple ValueError TypeError IndexError single handler

Form 3 — Alias form

Sometimes you want the actual exception details. Use as.

python
try:
    10 / 0

except ZeroDivisionError as err:
    print(err)

Output:

text
division by zero

Visual explanation

Form 3 ZeroDivisionError object stored in err variable

Professional use

python
except Exception as err:
    logger.error(err)

Used heavily in APIs, banking software, CRM applications, and FastAPI projects.


Form 4 — Bare except

python
try:
    risky()

except:
    print("Something went wrong")

Problem

Python catches everything. You do not know:

  • What failed
  • Why it failed
  • Which exception occurred

Visual warning

Form 4 bare except warning avoid in production MahiDev

Form 5 — Specific first, default last

This is the professional pattern.

python
try:
    a = int("10")
    c = a / 0

except ValueError:
    print("Invalid value")

except ZeroDivisionError:
    print("Cannot divide by zero")

except:
    print("Unknown error")

Why order matters?

Python checks handlers from top to bottom.

Form 5 specific handlers first default except last order

Form 6 — Generic Exception handler

Most common enterprise fallback.

python
try:
    risky_code()

except Exception as err:
    print("Error:", err)

Why Exception instead of bare except?

python
except Exception as err:

Because it:

  • Captures most application errors
  • Gives error details
  • Makes debugging easier

Enterprise logging example

python
try:
    response = api_call()

except Exception as err:
    logger.error(f"API failed: {err}")
Form 6 except Exception as err FastAPI Django Flask banking

Used extensively in FastAPI, Django, Flask, microservices, and banking APIs.


Comparison table

Form Syntax Recommended?
Form 1 except ValueError: ✅ Best
Form 2 except (A, B): ✅ Good
Form 3 except X as err: ✅ Best
Form 4 except: ⚠️ Avoid alone
Form 5 Specific + default ✅ Professional
Form 6 except Exception as err: ✅ Professional
Six forms summary MahiDev original diagrams

Memory trick

Think of a hospital.

Hospital specialists specific first general doctor last memory trick

Specific handlers first. Generic handler last.


Interview questions

Q1. Which form is most recommended?

Answer: Specific exception handlers (except ValueError) because they provide precise error handling.

Q2. Why use as err?

Answer: To access the exception object and its details for logging, debugging, and user messages.

Q3. Why avoid bare except?

Answer: It hides the real error and makes debugging difficult.

Q4. What is the enterprise fallback pattern?

Answer: except Exception as err: — captures most application exceptions while still exposing error details.


Key takeaways

  • Use specific exception handlers whenever possible
  • Use tuple syntax when multiple exceptions share the same handling
  • Use as err to access error information
  • Avoid bare except: as the only handler
  • Place generic handlers last
  • except Exception as err is the most common enterprise fallback pattern
  • Next: Custom Exceptions and raise — create business-rule exceptions with raise

Loading playground…

Check your understanding

Except block forms quiz

except (ValueError, TypeError): is form:

1 / 10