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. 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. Abhi
    Replies:
    2
    Views:
    725
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    464
    E. Robert Tisdale
    May 6, 2005
  3. Peter
    Replies:
    34
    Views:
    1,912
    James Kanze
    Oct 17, 2009
  4. wheres pythonmonks

    Re: inline exception handling in python

    wheres pythonmonks, Aug 12, 2010, in forum: Python
    Replies:
    0
    Views:
    637
    wheres pythonmonks
    Aug 12, 2010
  5. VSK
    Replies:
    0
    Views:
    237
Loading...

Share This Page