在以下代码段中,我得到的响应为-

__main__.CheckSumNotMatched: ('Kirti matches with - ', 'Kirti', ' . They are same.')

class CheckSumNotMatched(Exception):
    """Raised when the remote file's checksum doesn't match with local file checksum"""
    pass

def test(name):
    try:
        if name=="Kirti":
            error_msg = 'Kirti matches with - ', name, '. They are same. '
            raise CheckSumNotMatched(error_msg)
    except CheckSumNotMatched as csnm:
        logger.error(csnm)


if __name__ == "__main__":
    test("Kirti")


我希望回复为-
__main__.CheckSumNotMatched: Kirti matches with - Kirti. They are same.

我不想要(和'的回应。
正确的方法应该是什么?

最佳答案

你可以更换

error_msg = 'Kirti matches with - ', name, '. They are same. '




error_msg = 'Kirti matches with - ' + name + '. They are same. '

关于python - Python自定义异常未以正确的语法提供消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58620044/

10-12 16:36