There is the error in the except
block that's been mentioned already:
def final(x):
try:
print(x*x)
except:
print("Error")
damnit
finally:
print("End Function")
def dedent(x):
try:
print(x*x)
except:
print("Error")
damnit
print("End Function")
try:
final("a")
except Exception as e:
print("There was an error:", e)
#>>> Error
#>>> End Function
#>>> There was an error: global name 'damnit' is not defined
try:
dedent("a")
except Exception as e:
print("There was an error:", e)
#>>> Error
#>>> There was an error: global name 'damnit' is not defined
There's also return
behaviour:
def final(x):
try:
print(x*x)
except:
print("Error")
return "Return inside"
finally:
print("End Function")
return "Return outside"
def dedent(x):
try:
print(x*x)
except:
print("Error")
return "Return inside"
print("End Function")
return "Return outside"
try:
final("a")
except Exception as e:
print("There was an error:", e)
#>>> Error
#>>> End Function
#>>> 'Return outside'
try:
dedent("a")
except Exception as e:
print("There was an error:", e)
#>>> Error
#>>> 'Return inside'