What is an AssertionError? In which case should I throw it from my own code?

后端 未结 5 1555

In Item 2 of the \"Effective Java, 2nd edition\" book, there is this snippet of code, in which the author wants to forbid the empty initialization of an object.



        
5条回答
  •  伪装坚强ぢ
    2020-12-12 20:19

    An assertion Error is thrown when say "You have written a code that should not execute at all costs because according to you logic it should not happen. BUT if it happens then throw AssertionError. And you don't catch it." In such a case you throw an Assertion error.

    new IllegalStateException("Must not instantiate an element of this class")' // Is an Exception not error.
    

    Note: Assertion Error comes under java.lang.Error And Errors not meant to be caught.

提交回复
热议问题