簡體   English   中英

為什么異常沒有打印在except塊中

[英]Why exception isn't printed in except block

我正在嘗試從一個 except 塊中斷言一條消息,如下所示:

assert exceptions.main() == f"Join into Exception: {exceptions.CustomException}"

我正在使用這些文件:

# main file
class CustomException(Exception):
    """
    This is a custom exception
    """

def return_an_exception():
    pass

def another_func():
    pass

def main():
    try:
        return_an_exception()
        another_func()
    except CustomException as e:
        return f"Join into Exception: {str(e)}"
    return "Not join into Exception"
# test file
import exceptions


def test_exceptions(
    mocker,
):
    mocker.patch("exceptions.return_an_exception").side_effect = exceptions.CustomException 
    mocker.patch("exceptions.another_func")
    assert exceptions.main() == f"Join into Exception: {exceptions.CustomException}"

我收到此錯誤:

========================================================================================== FAILURES ==========================================================================================
______________________________________________________________________________________ test_exceptions _______________________________________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x102413f40>

    def test_exceptions(
        mocker,
    ):
        mocker.patch("exceptions.return_an_exception").side_effect = exceptions.CustomException
        mocker.patch("exceptions.another_func")
>       assert exceptions.main() == f"Join into Exception: {exceptions.CustomException}"
E       assert 'Join into Exception: ' == "Join into Ex...omException'>"
E         - Join into Exception: <class 'exceptions.CustomException'>
E         + Join into Exception:

test_exceptions.py:9: AssertionError
================================================================================== short test summary info ===================================================================================
FAILED test_exceptions.py::test_exceptions - assert 'Join on Exception: ' == "Join on Exce...omException'>"
===================================================================================== 1 failed in 0.06s ======================================================================================

你能幫我理解為什么不打印e

那是因為Exception__str__方法的默認實現是返回其消息:

print(Exception())                    # Prints nothing
print(Exception("An error occured"))  # Prints "An error occured"

您可以通過打印e.__class__.__name__來解決此問題:

class CustomException(Exception):
    pass

def return_an_exception():
    raise CustomException

def another_func():
    pass

def main():
    try:
        return_an_exception()
        another_func()
    except CustomException as e:
        return f"Join into Exception: {e.__class__.__name__}"
    return "Not join into Exception"
    
print(main())  # Now prints "Join into Exception: CustomException"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM