samedi 27 juin 2015

Is it bad form to count on exceptions?

My current situation is involved in making a game in python using a library called pygame. I have a method called getTile() that returns the tile every time the player moves. The lvlDict makes up the world.

def getTile(self, pos):
    x = pos[0]
    y = pos[1]
    return self.lvlDict[x,y].kind

and i'm thinking about changing it to this

def getTile(self, pos):
    try:
        x = pos[0]
        y = pos[1]
        return self.lvlDict[x,y].kind
    except KeyError:
        return None

where there would be some interpretation for what to do if it were None. Likely just move back to where it previously was. Is this inherently bad or acceptable? Even if it's just a matter of opinion, I'd like to know what people think about it.

Aucun commentaire:

Enregistrer un commentaire