When to use an assertion and when to use an exception

前端 未结 11 2081
暖寄归人
暖寄归人 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:43

    See section 6.1.2 (Assertions vs. other error code) of Sun's documentation at the following link.

    http://www.oracle.com/technetwork/articles/javase/javapch06.pdf

    This document gives the best advice I've seen on when to use assertions. Quoting from the document:

    "A good rule of thumb is that you should use an assertion for exceptional cases that you would like to forget about. An assertion is the quickest way to deal with, and forget, a condition or state that you don’t expect to have to deal with."

提交回复
热议问题