What are the implications of using assert vs. raise Exception

后端 未结 1 1981
深忆病人
深忆病人 2020-12-31 09:49

Are there any significant differences with the following?

raise Exception(\"some exception\")

assert False, \"some exception\"
相关标签:
1条回答
  • 2020-12-31 10:36

    Assertions can be disabled with the -O flag when starting Python. For this reason, use assertions only for sanity-checking, not for checking that is part of your program logic.

    Besides this, there is of course the difference that assertions raise AssertionError, which you really shouldn't catch. When you raise an exception, you can make the type of the exception appropriate to the error and catch it later.

    0 讨论(0)
提交回复
热议问题