首页 > 解决方案 > Python custom exception not giving message in proper syntax

问题描述

In the following code snippet, I get the response as -

__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")

I want the response as - __main__.CheckSumNotMatched: Kirti matches with - Kirti. They are same.

I don't want ( and ' in the response. What should be the right way to do that?

标签: pythonexception

解决方案


你可以更换

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

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

推荐阅读