Reputation: 1928
In this example I call fun3()
in fun2()
, but if something special happen in fun3()
is there a way to directly return
to fun1()
? without having to check this special thing in fun2()
.
def fun1(num):
res = fun2(num)
# do something if fun3() return an error...
def fun2(num):
res = fun3(num) # if fun3(num) return a status:error return to fun(1)
# something else...
def fun3(num):
if type(num) is not int:
return {'status': 'error'}
else:
# something else...
Upvotes: 0
Views: 42
Reputation: 1161
This is what exceptions are for
def fun1(num):
try:
res = fun2(num)
except Exception:
# specific exception handling
# will continue executing
# something else...
def fun2(num):
res = fun3(num) # if fun3(num) return a status:error return to fun(1)
# something else...
def fun3(num):
if type(num) is not int:
raise Exception('Status Error')
else:
# continue
You should consider using a specific Exception type, rather than just Exception
. Fitting for this problem would be a TypeError
.
Upvotes: 4