Assert keyword in Java

前端 未结 5 720
误落风尘
误落风尘 2020-12-07 11:13

Do you use the assert keyword or throw some validation runtime exception? What benefits does it give to you or why do you think it\'s not worth it to use?

5条回答
  •  一生所求
    2020-12-07 11:56

    To be precise on your question :

    Assert is used to validate the condition of a statement.

    assert (some condition)
    Example : assert (6 < 7) // condition pass
              assert (6 > 7) // throws AssertionException here
    

    Most people use assert for the following use case for String : (But I personally hate to use assert for it) :

    assert (obj != null || obj.isEmpty() || ... )
    

    I rather like using google guava which is clean and serve the purpose :

    Obj.isNullOrEmpty()
    

    More info : http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Strings.html

提交回复
热议问题