Chrome Extension

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")
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
Output
Repr Error : NameError("name 'some_undefined_method' is not defined",)
Error : name 'some_undefined_method' is not defined
Finally always executed
Was this helpful?
0 Comments
Join Devsheet Ask a Question