When to use an assertion and when to use an exception

前端 未结 11 2127
暖寄归人
暖寄归人 2020-11-27 09:28

Most of the time I will use an exception to check for a condition in my code, I wonder when it is an appropriate time to use an assertion?

For instance,



        
11条回答
  •  情书的邮戳
    2020-11-27 09:57

    Remember assertions can be disabled at runtime using parameters, and are disabled by default, so don't count on them except for debugging purposes.

    Also you should read the Oracle article about assert to see more cases where to use - or not to use - assert.

提交回复
热议问题