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

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -