2
爲什麼我得到以下兩個代碼段不同的結果(Python的3.4):傳遞關鍵字參數到自定義異常 - 異常
class MainError(Exception):
def __init__(self, msg, **parms):
super().__init__()
self.msg = msg
self.parms = parms
print('Parms original', parms)
def __str__(self):
return self.msg + ':' + str(self.parms)
class SubError(MainError):
def __init__(self, msg, **parms):
super().__init__(msg, **parms)
try:
raise SubError('Error occured', line = 22, col = 11)
except MainError as e:
print(e)
>>>
Parms original {'line': 22, 'col': 11}
Error occured:{'line': 22, 'col': 11}
和:
class MainError(Exception):
def __init__(self, msg, **args):
super().__init__()
self.msg = msg
self.args = args
print('Parms original', args)
def __str__(self):
return self.msg + ':' + str(self.args)
class SubError(MainError):
def __init__(self, msg, **args):
super().__init__(msg, **args)
try:
raise SubError('Error occured', line = 22, col = 11)
except MainError as e:
print(e)
>>>
Parms original {'line': 22, 'col': 11}
Error occured:('line', 'col')