inline exception handling in python

Discussion in 'Python' started by wheres pythonmonks, Aug 12, 2010.

  1. Hi!

    I have on a few occasions now wanted to have inline-exception
    handling, like the inline if/else operator.

    For example,

    The following might raise ZeroDivisionError:

    f = n / d

    So, I can look before I leap (which is okay):

    f = float("nan") if d == 0 else n/d;

    But, what I'd like to be able to write is:

    f = n / d except float("nan");

    Which I find much more appealing than:

    try:
    f = n / d
    except:
    f = float("nan")

    (Obviously, I am thinking about more complicated functions than "n/d"
    -- but this works as an example.)

    Thoughts?

    W
     
    wheres pythonmonks, Aug 12, 2010
    #1
    1. Advertisements

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. Abhi
    Replies:
    2
    Views:
    963
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    732
    E. Robert Tisdale
    May 6, 2005
  3. Replies:
    3
    Views:
    632
  4. Daniel Vallstrom
    Replies:
    2
    Views:
    2,780
    Kevin Bracey
    Nov 21, 2003
  5. Nish
    Replies:
    4
    Views:
    753
    Thomas Stegen
    Oct 8, 2004
  6. MarkE
    Replies:
    0
    Views:
    797
    MarkE
    Mar 27, 2007
  7. Peter
    Replies:
    34
    Views:
    2,387
    James Kanze
    Oct 17, 2009
  8. VSK
    Replies:
    0
    Views:
    385
Loading...