try catch - Best way to write "try elsetry" in python? -


i know 'tryelse' not real thing, question best way write out logic. given following example, foo , bar functions break.

i want aa foo(), if breaks, want become bar(), if 1 breaks too, set aa 0 default.

try:     aa = foo() elsetry:     aa = bar() except e:     aa = 0 

restating question, best real way write out logic in python?

the nested approach still best:

try:     aa = foo() except exception:     try:         aa = bar()     except exception:         aa = 0 

despite trying less nesting, above expresses wish , it's clear reader. if try nest more becomes awkward write , that's time rethink approach. nesting 2 try/excepts fine.

you can write:

try:     aa = foo() except exception:     aa = none  if aa none:     try:         aa = bar()     except exception:         aa = 0 

but somehow doesn't right (to me @ least). incorrect in case foo() can return none valid value aa.


Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -