mertz@gnosis.cx

Discussion in 'Python' started by Scott David Daniels, Jan 2, 2005.

  1. 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
     
    Scott David Daniels, Jan 2, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Mertz

    What's new with Gnosis

    David Mertz, Jul 12, 2003, in forum: Python
    Replies:
    3
    Views:
    335
    Bruno Desthuilliers
    Jul 13, 2003
  2. David Mertz, Ph.D.

    [Announce] Gnosis Utils 1.1.0 (update)

    David Mertz, Ph.D., Jul 16, 2003, in forum: Python
    Replies:
    0
    Views:
    331
    David Mertz, Ph.D.
    Jul 16, 2003
  3. Lulu of the Lotus-Eaters

    [Announce] Gnosis Utils 1.1.0 (update)

    Lulu of the Lotus-Eaters, Jul 16, 2003, in forum: Python
    Replies:
    0
    Views:
    307
    Lulu of the Lotus-Eaters
    Jul 16, 2003
  4. David Mertz, Ph.D.

    [Announce] Gnosis Utils 1.1.1

    David Mertz, Ph.D., Feb 19, 2004, in forum: Python
    Replies:
    0
    Views:
    381
    David Mertz, Ph.D.
    Feb 19, 2004
  5. David Mertz, Ph.D.

    Article on updates to gnosis.xml.objectify

    David Mertz, Ph.D., Dec 4, 2004, in forum: Python
    Replies:
    0
    Views:
    294
    David Mertz, Ph.D.
    Dec 4, 2004
Loading...

Share This Page