473,498 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mertz@gnosis.cx

I was pointed to appendix A by a comp.lang.py post.
In the "except statements" section (which might better be called
the "try" statement and include a try: ... finally: ...), you say:

The 'except' statement can optionally bind a name to an exception
argument:
try: ... raise "ThisError", "some message"
... except "ThisError", x: # Bind 'x' to exception argument
... print x
...
some message

String exceptions should not be encouraged (nor do I think they work).
Better code would be:
class MyError(Exception): pass
try: ... raise MyError, "some message"
... except MyError, x: # Bind 'x' to exception instance
... print x
...
some message

or, if you don't want two statements:
try: ... raise ValueError, "some message"
... except ValueError, x: # Bind 'x' to exception instance
... print x
...
some message
The x, by the way, is bound to an instance of the class of the
exception, and it has a field "args" which will reflect the
arguments with which the exception was created. So in these
cases, x.args is ('some message',) and if the code were:
try:

... raise ValueError("some message", 42)
... except ValueError, x: # Bind 'x' to exception instance
... print x
...
('some message', 42)

and x.args would be: ("some message", 42)
--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #1
0 997

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.