python
Use of try, except, else and finally in python
To handle exceptions that occurred in python code, you can use try-except in python. The else statement can also be used to verify that the code was executed successfully.
try:
some_undefined_method()
except Exception as e:
print("Repr Error : " + repr(e))
print("Error : " + str(e))
else:
print("Program Executed successfully")
finally:
print("Finally always executes")
Output
Repr Error : NameError("name 'some_undefined_method' is not defined",)
Error : name 'some_undefined_method' is not defined
Finally always executed
Error : name 'some_undefined_method' is not defined
Finally always executed
Was this helpful?
Similar Posts