basic python questions

John Machin sjmachin at lexicon.net
Sun Nov 19 07:44:58 CET 2006


nateastle at gmail.com wrote:
> So I implemented the exception spcified and in testing it returns:
>
> DeprecationWarning: raising a string exception is deprecated
>
> I am not to worried about depreciation warning however, out of
> curiosity, what would the better way be to handle this? Is there a way
> that (web site, help documentation, etc...) I would be able to find
> this? I am running this in Python 2.5

Just try shortening the statement to the bare:
   raise

For example:

| >>> try:
| ...    f = open("nonesuch.txt")
| ... except IOError:
| ...    raise
| ...
| Traceback (most recent call last):
|   File "<stdin>", line 2, in <module>
# Coming from a file you'll get filename, linenumber, function/method
above
| IOError: [Errno 2] No such file or directory: 'nonesuch.txt'
| >>>

If you feel that the error message that you get is descriptive enough,
even better than what you'd contemplated writing yourself, you're done.
Otherwise you need to raise an instance of the Exception class, and the
degree of difficulty just went up a notch.

[Aside] How are you going to explain all this to your instructor, who
may be reading all this right now?

Cheers,
John

>
> Diez B. Roggisch wrote:
> > nateastle at gmail.com schrieb:
> > > I have taken the coments and think I have implemented most. My only
> >
> > Unfortunately, no.
> >
> > > question is how to use the enumerator. Here is what I did, I have tried
> > > a couple of things but was unable to figure out how to get the line
> > > number.
> > >
> > > def Xref(filename):
> > >     try:
> > >         fp = open(filename, "r")
> > >     except:
> > >         raise "Couldn't read input file \"%s\"" % filename
> >
> > You still got that I-catch-all-except in there.
> > This will produce subtle bugs when you e.g. misspell a variable name:
> >
> > filename = '/tmp/foo'
> > try:
> >     f = open(fliename, 'r')
> > except:
> >     raise "can't open filename"
> >
> >
> > Please notice the wrong-spelled 'fliename'.
> >
> > This OTOH will give you more clues on what really goes wrong:
> >
> >
> >
> > filename = '/tmp/foo'
> > try:
> >     f = open(fliename, 'r')
> > except IOError:
> >     raise "can't open filename"
> > 
> > 
> > Diez




More information about the Python-list mailing list