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')
网友评论