inline exception handling in python

W

wheres pythonmonks

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
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top