My question is where the difference between
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
and
try:
print("Hello")
print("Nothing went wrong")
except:
print("Something went wrong")
lies. The result seems identical to me. What are use cases to use the else statement?
Similarly, why would I ever use the finally
statement?
try:
print(x) #Not defined before
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
and
try:
print(x) #Not defined before
except:
print("Something went wrong")
print("The 'try except' is finished")
again seems identical.
Edit: I do know, what 'else' and 'finally' do. I just don't know when to use them as it seems you can reach an equivalent result without these statements.