小编nep*_*eph的帖子

Pythonic 处理“”“三重引号””字符串中的前导空格...在异常 __init__() 期间修改用户创建的异常的参数?

我试图修改引发异常时构造的消息,但令我惊讶的是,传递的实际 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)

python string exception multiline

0
推荐指数
1
解决办法
337
查看次数

标签 统计

exception ×1

multiline ×1

python ×1

string ×1