什么是断言错误? 在哪种情况下我应该从我自己的代码抛出它?

在“高效 Java,第二版”一书的第2项中,有一段代码,作者希望禁止对象的空初始化。

class Example {
private Example() {
throw new AssertionError();
}
}

抛出异常的类型,是这里让我感到困惑的地方。

我不明白,如果 AssertionError被抛出只是因为没有更适合的错误或因为它应该是这样。

据我所知,这个错误是由框架在 assert语句失败时抛出的。而且,在 javadoc 中它只是编写

[抛出 AssertionError ]表示断言失败。

但是我没有看到任何断言(真假陈述)在这里被违反。 当然,“ You shall not instance ate an item Of this class”语句被违反了,但是如果这是其背后的逻辑,那么我们都应该到处抛出 AssertionError,而这显然不会发生。

FWIW,我刚刚扔了一个

new IllegalStateException("Must not instantiate an element of this class")

有什么问题吗? 在哪种情况下,我应该抛出一个 AssertionError在我自己的代码?

对不起,如果这只是一个微妙的怀疑,但我使用这种模式在我的代码很多,我想确保我正在做正确的事情。

313168 次浏览

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.

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.

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.

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.

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.

assert len(tf.config.list_physical_devices('GPU')) > 0

this will not work if you are using the collab

use Jupyter Notebook to use physical devise GPU