0Pricing
Flask Academy · Lesson

Raise Custom Exception Classes

Define domain errors that map to JSON.

Raise Custom Exception Classes is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Errors That Speak Your Domain

Numbers like 400 are generic. A custom exception names the real problem, such as a payment that failed or a quota hit.

Subclass an Exception

You start by defining a class that inherits from Exception. This gives you a clear, raisable error of your own.

class PaymentError(Exception):
    pass

Add a Status Code

Give the class a status_code attribute so a handler knows which HTTP code to send when it fires.

class PaymentError(Exception):
    status_code = 402

Carry a Message

Store a human message in the constructor. Your handler can later read it and place it into the response body.

def __init__(self, message):
    self.message = message

Raise It in a View

Inside a view you simply raise the error when something goes wrong. Flask then looks for a matching handler.

if not paid:
    raise PaymentError("Card declined")

Handle the Custom Class

Register an errorhandler for your class. It receives the instance, so you can read both its message and status code.

@app.errorhandler(PaymentError)
def on_pay(e):
    return e.message, e.status_code

Return JSON From It

For an API, turn the error into JSON inside the handler. Clients get a structured failure they can parse reliably.

return jsonify(error=e.message), e.status_code

One Base for Many Errors

Make a single base class and let specific errors subclass it. Then one handler can cover your whole error family.

class ApiError(Exception):
    pass

Default Sensible Values

Give the base a default status_code like 400. Subclasses override it only when they need a different one.

class ApiError(Exception):
    status_code = 400

Keep Logic Out of Views

Raising domain errors lets views stay short. The handler owns formatting, so each view just signals what went wrong.

Why This Scales

As an app grows, custom exceptions keep error logic in one tidy spot. New error types plug into the same clean pipeline.

Quick Check

How does a view trigger your domain error?

Recap

You built custom exception classes with a status code and message, raised them in views, and mapped them to clean JSON. Awesome!

Frequently asked questions

Is the “Raise Custom Exception Classes” lesson free?

Yes — the full text of “Raise Custom Exception Classes” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Raise Custom Exception Classes”?

Define domain errors that map to JSON. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Raise Custom Exception Classes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. abort and HTTP Error Codes
  2. Register errorhandler Functions
  3. Raise Custom Exception Classes
  4. Uniform JSON Error Envelopes
← Back to Flask Academy