Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:08:16+00:00 2026-06-14T07:08:16+00:00

I find myself often wanting to structure my exception classes like this: # legends.py

  • 0

I find myself often wanting to structure my exception classes like this:

# legends.py
class Error(Exception): pass

class Rick(object):
    class Error(Error): pass
    class GaveYouUp(Error): pass
    class LetYouDown(Error): pass

class Michael(object):
    class Error(Error): pass
    class BlamedItOnTheSunshine(Error): pass
    class BlamedItOnTheMoonlight(Error): pass

I have only seen this pattern used in Django (DoesNotExist) and it makes so much sense. Is there anything I’m missing, why most people seem to favor top-level Exceptions?

edit
I would use these classes for versatile granularity, e.g:

import legends

try:
    do_stuff()
except legends.Michael.Error:
    blame_it_on_the_boogie()
except legends.Rick.GaveYouUp:
    let_you_down()
except legends.Error:
    pass
except Exception as e:
    raise Hell()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:08:17+00:00Added an answer on June 14, 2026 at 7:08 am

    This is the exact pattern used by Django for certain ORM-related exceptions.

    The advantage is that you can have an except clause which checks against a type accessed through an instance:

    rick = Rick()
    
    try:
       rick.roll()
    except rick.GaveYouUp:
       never()
    except rick.LetYouDown:
       never_ever()
    

    This doesn’t look that useful here, but if rick were a function parameter, then it would potentially be rather useful.

    This is also extremely useful in writing generic code which raises the exceptions:

    GoddamStar(object):
       def sing(self,tune):
           raise self.Error()
    
    class Rick(GoddamStar):
        class Error(Error): pass
        class GaveYouUp(Error): pass
        class LetYouDown(Error): pass
    
    class Michael(GoddamStar):
        class Error(Error): pass
        class BlamedItOnTheSunshine(Error): pass
        class BlamedItOnTheMoonlight(Error): pass
    
    rick = Rick()
    
    try:
       rick.sing()
    except Rick.GaveYouUp:
       never()
    except Michael.Error:
       never_ever()
    

    Django’s exceptions generally all derive from global base classes, so that you can also have a catch-all clause which still switches on a type of exception, in case your rick is of an unknown (or otherwise unprovided for) class.

    The reason why this isn’t much more common is that (a) it doesn’t work in early-bound languages, which attract most of the book writers (b) it’s moderately rare that this is useful to the user, and so application writers likely figure they aren’t going to need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself often wanting to write Python list comprehensions like this: nearbyPoints =
I often find myself wanting to do something like this, I have something wrapped
Quite often, I find myself wanting a simple, dump object in Python which behaves
I often find myself wanting to insert regular functions into a binded sequence. Like
I often find myself doing things like this when manipulating tables:- $($('table tr').children()[2]).html(); For
This is a very simple question. I often find myself wanting to create a
I often find myself wanting (and then usually writing) Scalaz type class instances for
I often find myself wanting to write an SQL query like the following: SELECT
I really like vim's fuzzyfinder plugin, but I often find myself wanting to open
One thing I find myself often wanting to do is convert a large associative

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.