Reputation: 2273
I have the following functions:
def outer_function():
return 'It worked'
def fun():
try:
return function1()
except:
return function2()
When executing fun()
I would like to check that if function1()
works correctly, then fun()
can return an array such as this
[function1(),outer_function()]
How could I check whether function1()
works inside ´fun()
and return an output such as the desired one?
Upvotes: 1
Views: 98
Reputation: 1644
Instead of return function1()
, do return [function1(), outer_function()]
So your function would be:
def fun():
try:
return [function1(), outer_function()]
except:
return function2()
If function1()
raises an error, the array will NOT be returned. Instead, the try block will catch the Exception.
NOTE: This code is contingent on the fact that function2
and outer_function
will not raise any errors of their own.
Upvotes: 1
Reputation: 373
I have tried your question and made it on 'Repl.it' Online Compiler. Here Your Link
def function1():
try:
return True
except :
return False
def function2():
try:
return True
except:
return False
def outer_function():
return 'It worked'
def fun():
if function1():
return [function1(), outer_function()]
else:
return function2()
print(fun())
Upvotes: 0
Reputation: 1386
You can create a variable for function1 output, and if function1 throws an error it will stop the return statement and go into the catch/except. Otherwise it will go to the else statement and return the desired array:
def outer_function():
return 'It worked'
def fun():
try:
x = function1()
except:
return function2()
else:
return [x, outer_function()]
Upvotes: 2