[Tutor] exceptional behaviour
paul
polb@cableinet.co.uk
Thu, 24 Jan 2002 08:11:30 -0000
Date sent: Wed, 23 Jan 2002 11:00:55 -0800
To: "paul" <polb@cableinet.co.uk>
From: Kirby Urner <urnerk@qwest.net>
Subject: Re: [Tutor] exceptional behaviour
Copies to: tutor@python.org
Here's the code for the catcher:
import raiserr
try:
raiserr.div42()
except "problem":
print "number must be greater than zero"
except "problem 2":
print"42, obviously"
Here's the code for the thrower (raiserr.py):
def div42():
denominator=input("What value will I divide 42 by?\n")
if denominator == 0:
raise "problem"
elif denominator == 1:
raise "problem 2"
else:
print 42/denominator
the first exception works fine, the second gives me this:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in ?
raiserr.div42()
File "D:\Python21\raiserr.py", line 6, in div42
raise "problem 2"
problem 2
removing the space from the raise string allows it to work just fine,
it's not a problem, but it's hard to see why it should differentiate
between strings with spaces and those without.
cheers
Paul
>
> >
> >def div42():
> > denominator=input("What value will I divide 42 by?\n")
> > if denominator == 0:
> > raise "first problem"
> > elif denominator == 1:
> > raise "second problem"
> > else:
> > print 42/denominator
> >
> >doesn't
>
> What error message to you get for the 2nd example?
> Seems to work fine in 2.2:
>
> >>> div42()
> What value will I divide 42 by?
> 0
> Traceback (most recent call last):
> File "<pyshell#16>", line 1, in ?
> div42()
> File "<pyshell#15>", line 4, in div42
> raise "first problem"
> first problem
>
> Kirby
>