import sys
class MyError(Exception):
def __init__(self,value):
self.value = value
def __str__(self):
return repr(self.value)
try:
f = open('new.in')
s = f.readline()
f.close()
i = int(s.strip())
raise MyError(3*3)
except IOError as e:
print "I/O error({0}):{1}".format(e.errno , e.strerror)
except ValueError:
print "Could not convert string to int"
except MyError as e:
print "My errro occurred,value:",e.value
except:
print "Unexpected error:",sys.exc_info[0]
raise
参考 :
http://docs.python.org/2/tutorial/errors.html