, Python, , , ++ Java, - Python, , " , " ( , , , ). , except - ( , , raise, ). , , , , :
expected_exceptions = KeyError, AttributeError, TypeError
except expected_exceptions:, except:.
, , :
try:
foo1()
except expected_exceptions:
try:
if condition:
foobetter()
else:
raise
except expected_exceptions:
handleError()
try/except:
def may_raise(expected_exceptions, somefunction, *a, **k):
try:
return False, somefunction(*a, **k)
except expected_exceptions:
return True, None
, - "". , ( , ), :
failed, _ = may_raise(expected_exceptions, foo1)
if failed and condition:
failed, _ = may_raise(expected_exceptions, foobetter)
if failed:
handleError()
, , , . , , may_raise, , ( , , , ); ...! -)