python允许程序员自定义异常,用于描述python中没有涉及的异常情况,自定义异常必须继承Exception类,自定义异常按照命名规范以"Error"结尾,显示地告诉程序员这是异常。自定义异常使用raise语句引发,而且只能通过人工方式触发。
Python中自定义异常类,继承Exception类即可:class NetworkError(Exception): pass
如果需要重载__init__()方法,则需要提供两个参数:class DeviceError(Exception):
def __init__(self,errno,msg):
self.args = (errno, msg)
self.errno = errno
self.errmsg = msg
# Raises an exception (multiple arguments)
raise DeviceError(1, 'Not Responding')
本文来自投稿,不代表访得立场,如若转载,请注明出处:http://www.found5.com//view/1281.html