我试图修改引发异常时构造的消息,但令我惊讶的是,传递的实际 arg 是捕获的内容except,而不是异常内部的任何值。
这就是我想做的:
class ScriptError(Exception):
"""This is our special exception class so we can catch it &
nicely print it in a popup message.
"""
def ___init___(self, message):
self.message = self.clean(message)
def clean(message):
"""This clears whitespace at the start of each line for multiline strings.
Required for the big triple-quote strings to both be formatted nicely in the
code and also render properly in the tkinter popups.
"""
return '\n'.join([line.lstrip() for line in str(message).split('\n')])
Run Code Online (Sandbox Code Playgroud)
我尝试这样做是因为这种类型的异常往往是多行生物:
if not …Run Code Online (Sandbox Code Playgroud)