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 8768541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:05:45+00:00 2026-06-13T17:05:45+00:00

I’m new to Python and I’m not sure what’s the best place to put

  • 0

I’m new to Python and I’m not sure what’s the best place to put my user-defined exceptions, and where to catch them.

For example, I have a program which starts a game, when the game is stopped, an user-defined exception is raised (StopGame). I should catch this exception from outside the game since the game is throwing this exception. Though, the exception is specific to the game so the definition of the exception should be in the game module (or maybe the game package?).

Here is the simple layout of the program:

Program structure:

__main__.py
game/
    __init__.py
    game.py

__main__.py

import game

def main():
    g = game.Game()

    try:
        g.start()
    except game.StopGame:
        print '\nbye bye!'

if __name__ == '__main__':
    main()

game/__init__.py

from game import Game
from game import StopGame

game/game.py

class Game:

    def start(self):

        try:
            a = raw_input('do you want to play?\n')
            print 'cool'
        except (KeyboardInterrupt, EOFError):
            raise StopGame

        if a == 'no':
            raise StopGame
        else:
            print 'cool'

class StopGame(Exception):
    pass

This code works fine like this, I’m just not sure this is the way to go. What I find somewhat disappointing is that I should import every exception in game/_init_.py using this setup. Otherwise I should catch the exception like:

except game.game.StopGame

Which looks a bit nasty. I think it’s better to be able to reach all the attributes in the module ‘game’ from the variable ‘game’ in _main_.py. Then I know you can put this in game/_init_.py:

from game import *

Which will import all classes from game/game.py, but I heard that using * for importing is a bad practice.

So, my question is, what is the best way to do this? Maybe my whole setup is wrong, if so, I would like to hear what’s the correct setup.

Thanks a lot in advance!

BTW: If you might wonder why I have a game module inside a game package: the idea is that the package will contain a lot of more modules that are related to the game, game/game.py is just the initiator of the whole game, maybe I should put that code into game/_init_.py?

  • 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-13T17:05:46+00:00Added an answer on June 13, 2026 at 5:05 pm

    How is it done elsewhere?

    I think that the best way to answer your issue is to look at an actual Python package and see how it’s built. I’ll take the example of the excellent python-requests package.

    This module is about making HTTP requests. Yours is about playing a game.

    Basic HTTP requests functionality is imported in the requests/__init__.py file, but is defined elsewhere. That’s what you’re doing, but the ‘elsewhere’ could have a better name. Maybe game/core.py could be a good fit.

    Exceptions are defined in the requests/exceptions.py file. It’s usually appropriate for a relatively-small package to have all the exceptions in one place.
    Note that the exceptions are imported into requests/__init__.py too! This makes them easier to import in other packages that might need to catch them!

    Last, no from module import * is used. It’s not going to take so much time to actually add exactly what’s needed, so you should avoid *.


    What can you do in your case?

    • Avoid having a game/game.py file, call it game/core.py or something
    • For a small package, put your exceptions in a game/exceptions.py file
    • In game/__init__.py, import what’s usually needed from your package: the main classes and the exceptions.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported

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.