python try function except code example
Example 1: try catch python
try:
print("try to run this block")
except:
print("run this bock if there was error in earlier block")
Example 2: python try except
import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
print("OS error: {0}".format(err))
except ValueError:
print("Could not convert data to an integer.")
except:
print("Unexpected error:", sys.exc_info()[0])
raise