您的位置:首页 > 大数据 > 人工智能

Raise and Handle Exception Demo

2014-01-02 16:07 393 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: