断言不起作用
我正在尝试编写一个断言来检查用户给出的大小是否为正值,如果不是,则将其设为正值,此语句位于类构造函数内部,该构造函数获取大小值,然后创建一个数组[size]。我写了下面的代码,我相信它是正确的。
public Grid(int size) {
try{
assert size > 0 ;
}
catch(AssertionError e){
size = Math.abs(size);
}
setLayout(new GridLayout(size, size));
grid = new JButton[size][size];
}
虽然我似乎从未评估过我的断言并继续程序,然后导致 NegativeArraySize 错误(我试图避免),但
我也尝试过,
assert size>0;
并且程序因负值而无法停止。
我在运行 java 时遇到了一些问题最近使用 mac,所以我不知道我的代码是否正确,或者它是否只是那些奇怪的 mac 怪癖之一!并且应该使用
size=Math.abs(size);
谢谢山姆,
I am trying to write an Assertion to check if the size the user gives is a positive value, if not then make it positive, this statement is inside the class constructor which takes the size value and then makes an array[size]. I have written the below code which i believe to be correct.
public Grid(int size) {
try{
assert size > 0 ;
}
catch(AssertionError e){
size = Math.abs(size);
}
setLayout(new GridLayout(size, size));
grid = new JButton[size][size];
}
Though I never seems to evaluate my assertion and continues the program then causes the NegativeArraySize error( which i am trying to avoid)
I also tried just
assert size>0;
And the program fails to stop for negative values..
I have had a few problems with running java on mac recently, so i don't know if my code is right or if it is just one of those odd mac quirks!! and should just use
size=Math.abs(size);
Thanks Sam,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
-ea
开关(启用断言)运行程序,否则 JVM 根本不会运行assert
指令。依赖断言有点危险。我建议您这样做:或者甚至像这样:
第二个建议的好处是向您显示代码其他部分中潜在的编程错误,而第一个建议则默默地忽略它们。这取决于您的用例。
You need to run your program with the
-ea
switch (enable assertions), otherwise noassert
instructions will be run by the JVM at all. Depending on asserts is a little dangerous. I suggest you do something like this:Or even like this:
The second suggestion has the benefit of showing you potential programming errors in other parts of your code, whereas the first one silently ignores them. This depends on your use case.
断言可以在程序启动时启用或禁用,默认情况下是禁用的。
请参阅启用和禁用断言
简而言之,在除系统类之外的所有类中启用断言,请在运行类时使用
-enableassertions
或-ea
进行切换。Assertions can be enabled or disabled when the program is started, and are disabled by default.
See Enabling and Disabling Assertions
In short, to enable assertions in all classes, except System classes, use the
-enableassertions
, or-ea
, switch when you run your class.由于assert是JDK 1.4中引入的新Java关键字,因此您必须使用JDK 1.4编译器来编译程序。此外,您还需要在编译命令中包含开关 –source 1.4,如下所示:
注意:如果您使用 JDK 1.5 或更高版本,则无需在命令中使用 –source 1.4 选项。
默认情况下,断言在运行时被禁用。要启用它,请使用开关 –enableassertions,或简称为 –ea,如下所示:
可以在类级别或包级别有选择地启用或禁用断言。禁用开关是 –disableassertions 或简称为 –da。
例如,以下命令启用包 package1 中的断言并禁用类 Class1 中的断言。
断言不应该用来代替异常处理。异常处理处理程序执行过程中的异常情况。断言是为了保证程序的正确性。异常处理解决健壮性问题,断言解决正确性问题。与异常处理一样,断言不用于正常测试,而是用于内部一致性和有效性检查。
所以在这种情况下,最好的答案是异常处理。
不要在公共方法中使用断言进行参数检查。可以传递给公共方法的有效参数被视为该方法契约的一部分。无论断言是启用还是禁用,都必须始终遵守合同。例如,上面的代码应该使用异常处理来重写
Since assert is a new Java keyword introduced in JDK 1.4, you have to compile the program using a JDK 1.4 compiler. Furthermore, you need to include the switch –source 1.4 in the compiler command as follows:
NOTE: If you use JDK 1.5 or later, there is no need to use the –source 1.4 option in the command.
By default, the assertions are disabled at runtime. To enable it, use the switch –enableassertions, or –ea for short, as follows:
Assertions can be selectively enabled or disabled at class level or package level. The disable switch is –disableassertions or –da for short.
For example, the following command enables assertions in package package1 and disables assertions in class Class1.
Assertion should not be used to replace exception handling. Exception handling deals with unusual circumstances during program execution. Assertions are to assure the correctness of the program. Exception handling addresses robustness and assertion addresses correctness. Like exception handling, assertions are not used for normal tests, but for internal consistency and validity checks.
So In this case, best answer is exception handling.
Do not use assertions for argument checking in public methods. Valid arguments that may be passed to a public method are considered to be part of the method’s contract. The contract must always be obeyed whether assertions are enabled or disabled. For example, the above code should be rewritten using exception handling