Java 断言问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5688056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Assertion Question
提问by Student
What exactly happens when a Java assertion fails? How does the programmer come to know that an assertion has failed?
当 Java 断言失败时究竟会发生什么?程序员如何知道断言失败了?
Thanks.
谢谢。
采纳答案by Simon Nickerson
If assertions are enabled in the JVM (via the -ea
flag), an AssertionError
will be thrown when the assertion fails.
如果在 JVM 中启用了断言(通过-ea
标志),AssertionError
当断言失败时将抛出an 。
This should notbe caught, because if an assertion fails, it basically means one of your assumptions about how the program works is wrong. So you typically find out about an assertion failure when you get an exception stack trace logged with your thread (and possibly whole program) terminating.
这不应该被发现,因为如果断言失败,这基本上意味着您对程序如何工作的假设之一是错误的。因此,当您在线程(也可能是整个程序)终止时记录异常堆栈跟踪时,您通常会发现断言失败。
回答by Ravi Wallau
An assertion will only fail if you enabled assertions in the JVM when it started. You can do that by specifying the parameter -ea in the command line. If you do that, then this block of code will throw an AssertionError
when it is executed:
只有在 JVM 启动时启用了断言,断言才会失败。您可以通过在命令行中指定参数 -ea 来实现。如果你这样做,那么这段代码AssertionError
在执行时会抛出一个:
public void whatever() {
assert false;
}
Assertions should be used to detect programming errors only. If you are validating user input or something on those lines, don't use assertions.
断言应仅用于检测编程错误。如果您正在验证用户输入或这些行上的内容,请不要使用断言。
回答by QuantumMechanic
It throws an AssertionError
which is a subclass of Error
. As an Error
generally and as a failed assertion in particular, you likely should not try to catch it since it is telling you there's a significant abnormality in your code and that if you continue you'll probably be in some undefined, unsafe state.
它抛出一个AssertionError
,它是 的子类Error
。作为Error
一般和特别是失败的断言,您可能不应该尝试捕获它,因为它告诉您代码中存在重大异常,并且如果您继续,您可能会处于某种未定义的、不安全的状态。
回答by Larry Watanabe
It throws an AssertionError. Howeveer, you have to compile the program with the -ea or -enableassertions flag to have it generate an actual exception
它抛出一个断言错误。但是,您必须使用 -ea 或 -enableassertions 标志编译程序才能生成实际异常
回答by Thomas Jungblut
If an assertion fails and assertion are enabled at runtime it will throw an AssertionError.
Usually you use assert statements in JUnit testings, when building your application you are running a test utility that will check for errors and tell you.
如果断言失败并且在运行时启用了断言,它将抛出一个AssertionError。
通常您在 JUnit 测试中使用 assert 语句,当构建您的应用程序时,您正在运行一个测试实用程序,它将检查错误并告诉您。
Take a look at this: Programming With Assertions
看看这个:用断言编程
回答by no_ripcord
It throws an Error
. It is just like when you get a NullPointerException
, but it's a subclass of java.lang.Error
. The name is AssertionError
.
它抛出一个Error
. 就像你得到 a 一样NullPointerException
,但它是java.lang.Error
. 名字是AssertionError
。
It's like a NullPointerException
in the sense that you don't have to declare the throws or anything, it just throws it.
这就像 aNullPointerException
在某种意义上你不必声明抛出或任何东西,它只是抛出它。
assert(false);
is like
就好像
throw new AssertionError();
if you run your program with the -ea
flag passed to the java program (VM).
如果您使用-ea
传递给 java 程序 (VM)的标志运行您的程序。
回答by Nikhil
Programmer can write try catch block so if a error has occurred then it can be caught in catch and the programmer can come to know
程序员可以编写 try catch 块,因此如果发生错误,则可以在 catch 中捕获并且程序员可以知道
try
{
assert false;
}
catch(Exception e)
{
System.out.println("Error has occured");
}