Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python user-defined exceptions placement and catching

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?

like image 934
gitaarik Avatar asked Nov 01 '12 18:11

gitaarik


People also ask

How does Python handle user defined exceptions?

To generate a user defined exception, we use the “raise” keyword when a certain condition is met. The exception is then handled by the except block of the code. We then use pass statement. pass statement is used to show that we will not implement anything in our custom exception class.

Can we define user defined exceptions in Python?

In Python, users can define custom exceptions by creating a new class. This exception class has to be derived, either directly or indirectly, from the built-in Exception class. Most of the built-in exceptions are also derived from this class.

What are user defined exceptions explain with an example?

In simple words, we can say that a User-Defined Exception or custom exception is creating your own exception class and throwing that exception using the 'throw' keyword. For example, MyException in the below code extends the Exception class.

How can a user defined exception be raised?

Predefined exceptions are raised implicitly (automatically) by the runtime system. User-defined exceptions must be raised explicitly by RAISE statements. To handle raised exceptions, you write separate routines called exception handlers.


1 Answers

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.
like image 192
Thomas Orozco Avatar answered Oct 04 '22 02:10

Thomas Orozco