O'Reilly logo

Sage Beginner's Guide by Craig Finch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - creating custom exception types

We will finally give our tanks the ability to fire their cannons. Adding a fire method means that we will need to raise exceptions related to cannon fire, in addition to exceptions that come from movement. To handle this situation, we will define custom error classes. Create a file called exceptions.py in the combatsim directory and enter the following code:

 class CombatsimError(Exception): """Base class for exceptions generated by combatsim""" def __init__(self, value): """Create a CombatsimError exception. Arguments: value String describing the error """ Exception.__init__(self, value) class MoveError(CombatsimError): def __init__(self, value): CombatsimError.__init__(self,value) class ShootError(CombatsimError): ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required