• Thread starter Scott David Daniels
  • Start date
S

Scott David Daniels

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:
... 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:
... raise MyError, "some message"
... except MyError, x: # Bind 'x' to exception instance
... print x
...
some message

or, if you don't want two statements:
... 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:
... 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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top