0

I was looking at the documentation for try except and the built in values listed there, but I figure out what I should be using to catch a general function fail. here's what I mean say I have these: foo.py

def create_apples(x,y,z):
    appleMaker = appleMaker()
    appleMaker.maker(x,y,z)
def create_bananas(x,y,z):
    bananaMaker = BananaMaker()
    bananaMaker.maker(x,y,z)

if __name__ == '__main__':

    x = 1
    y = 2
    z = 3
    create_apples(x, y, z)
    create_bananas(x, y, z)

with appleMaker.py:

from random import randint

class appleMaker:
    def __init__(self):
        self.bushelID
        self.numberCreated

    def maker(x, y, z):
        self.bushelID = randint(0,9)
        self.numberCreated = x + y + z

and BananaMaker.py looking exactly the same as appleMaker.py respectively. What I want to be able to do is in foo, something like:

try:
    create_apple(x,y,z)
except Exception:
    print "informative information"
    sys.exit(1)
2

1 Answer 1

3

Generally you would read the documentation for create_apple() to see what exceptions it can raise, and catch those.

Unlike Java, though, Python functions aren't required to declare all the possible exceptions they can raise, so any function could conceivably raise many different exceptions.

Your best bet might be some sort of catchall condition at the end:

try:
    create_apple(x,y,z)

except NoTreeFound:
    print 'could not find an apple tree'

except BasketFull:
    print 'apple basket is already full of apples'

except Winter:
    print 'Cannot create apples in winter!'

except Exception as e:
    print 'an unknown error occurred, message is: %s' % str(e)

UPDATE

It appears you're looking for advice on how a function should raise exceptions, not how a caller should catch them.

If your function can fail in several distinct ways, then defining specific exceptions for each failure condition can be convenient for the caller, as it can easily handle each failure condition separately (as I showed in the example above.)

But if your function can really only fail in one way, with possibly slightly different details in some cases, then perhaps the best thing to do is raise a general exception with a specific message:

raise Exception("my coffee is too cold")

In the end it's a design decision. Both ways will work.

Sign up to request clarification or add additional context in comments.

3 Comments

down vote? I think it is a pretty straight forward question...@John Gordon, create_apple is a custom function in this case; as opposed to say os.path.join(dir1, dir2), where I could catch the OSError. So is what you are saying is that I need to define my own exceptions for class appleMaker()?
It sounds like you're writing a new function and trying to decide which exceptions it should raise. But your post title refers to catching, not raising...
Thanks John. The functions I'm dealing with are far more complicated than these contrived examples. You are right in that my question was more in line with how to raise exceptions than with catching them. I think I am going to have to create a little exception class for them and raise those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.