Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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):
    def __init__(self, value):
        CombatsimError.__init__(self,value)

Enter the following code in vehicle.py in the combatsim directory:

import...