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

后端 未结 5 1556

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:01

    AssertionError is an Unchecked Exception which rises explicitly by programmer or by API Developer to indicate that assert statement fails.

    assert(x>10);
    

    Output:

    AssertionError
    

    If x is not greater than 10 then you will get runtime exception saying AssertionError.

    0 讨论(0)
  • 2020-12-12 20:03

    I'm really late to party here, but most of the answers seem to be about the whys and whens of using assertions in general, rather than using AssertionError in particular.

    assert and throw new AssertionError() are very similar and serve the same conceptual purpose, but there are differences.

    1. throw new AssertionError() will throw the exception regardless of whether assertions are enabled for the jvm (i.e., through the -ea switch).
    2. The compiler knows that throw new AssertionError() will exit the block, so using it will let you avoid certain compiler errors that assert will not.

    For example:

        {
            boolean b = true;
            final int n;
            if ( b ) {
                n = 5;
            } else {
                throw new AssertionError();
            }
            System.out.println("n = " + n);
        }
    
        {
            boolean b = true;
            final int n;
            if ( b ) {
                n = 5;
            } else {
                assert false;
            }
            System.out.println("n = " + n);
        }
    

    The first block, above, compiles just fine. The second block does not compile, because the compiler cannot guarantee that n has been initialized by the time the code tries to print it out.

    0 讨论(0)
  • 2020-12-12 20:09

    The meaning of an AssertionError is that something happened that the developer thought was impossible to happen.

    So if an AssertionError is ever thrown, it is a clear sign of a programming error.

    0 讨论(0)
  • 2020-12-12 20:17

    Of course the "You shall not instantiate an item of this class" statement has been violated, but if this is the logic behind that, then we should all throw AssertionErrors everywhere, and that is obviously not what happens.

    The code isn't saying the user shouldn't call the zero-args constructor. The assertion is there to say that as far as the programmer is aware, he/she has made it impossible to call the zero-args constructor (in this case by making it private and not calling it from within Example's code). And so if a call occurs, that assertion has been violated, and so AssertionError is appropriate.

    0 讨论(0)
  • 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.

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