views:

11

answers:

1

Does python 2.5 allow you to pass exception arguments?

try: raise Exception("argument here")
except Exception: print Exception.args

I've had no luck with the above code - I know this is how you do it in Python 2.7 - is this not in Python 2.5?

+1  A: 

You aren't actually raising the exception, just creating it. Once you fix that, you also need to refer to the instance that gets raised, not just the Exception class:

>>> try: 
...     raise Exception('foo', 23)
... except Exception, e: 
...     print e.args
... 
('foo', 23)
Marcelo Cantos
I don't get that. I get: <attribute 'args' of 'exceptions.BaseException' objects>
ehfeng
Also, when I try your suggestion, as I "syntaxerror: invalid syntax" on the "as" keyword.
ehfeng
I'm using Python 2.6. It appears to have changed since 2.5, which surprised me more than a little. I've amended the answer.
Marcelo Cantos
Wow, thanks so much. I can't say how much endless frustration this would have caused me. I know I'm pushing my luck...but how did you figure this out? Was it in the Python 2.5 docs?
ehfeng
Yes (http://docs.python.org/release/2.5/ref/try.html), but the older language docs are pretty opaque to the causal reader. I saw the comma in the grammar and guessed.
Marcelo Cantos
Yikes. Glad I asked.
ehfeng