macos 断言不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7865241/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:31:46  来源:igfitidea点击:

Assertion not working

javamacosswingosx-snow-leopard

提问by Sam Palmer

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.

我正在尝试编写一个断言来检查用户给出的大小是否为正值,如果不是,则将其设为正值,此语句位于类构造函数中,该类构造函数获取大小值,然后创建一个数组 [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];
}

Though I never seems to evaluate my assertion and continues the program then causes the NegativeArraySize error( which i am trying to avoid)

虽然我似乎从来没有评估过我的断言并继续执行程序然后导致 NegativeArraySize 错误(我试图避免)

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

我最近在 mac 上运行 java 时遇到了一些问题,所以我不知道我的代码是否正确,或者它是否只是那些奇怪的 mac 怪癖之一!!并且应该只使用

size=Math.abs(size);

Thanks Sam,

谢谢山姆,

回答by Matt

You need to run your program with the -easwitch (enable assertions), otherwise no assertinstructions will be run by the JVM at all. Depending on asserts is a little dangerous. I suggest you do something like this:

您需要使用-ea开关运行您的程序(启用断言),否则assertJVM根本不会运行任何指令。依赖断言有点危险。我建议你做这样的事情:

public Grid(int size) {
    size = Math.max(0, size) 
    setLayout(new GridLayout(size, size));
    grid = new JButton[size][size];
}

Or even like this:

或者甚至像这样:

public Grid(int size) {
    if(size < 0) {
        throw new IllegalArgumentException("cannot create a grid with a negative size");
    } 
    setLayout(new GridLayout(size, size));
    grid = new JButton[size][size];
}

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.

第二个建议的好处是可以向您展示代码其他部分中潜在的编程错误,而第一个建议会默默地忽略它们。这取决于您的用例。

回答by stivlo

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.

简而言之,要在除 System 类之外的所有类中启用断言,请在运行类时使用-enableassertions, 或-ea切换。

回答by dulaj sanjaya

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:

由于 assert 是 JDK 1.4 中引入的新 Java 关键字,因此您必须使用 JDK 1.4 编译器来编译程序。此外,您需要在编译器命令中包含开关 –source 1.4,如下所示:

javac –source 1.4 AssertionDemo.java

NOTE: If you use JDK 1.5 or later, there is no need to use the –source 1.4 option in the command.

注意:如果您使用 JDK 1.5 或更高版本,则无需在命令中使用 –source 1.4 选项。

By default, the assertions are disabled at runtime. To enable it, use the switch –enableassertions, or –eafor short, as follows:

默认情况下,断言在运行时被禁用。要启用它,请使用开关–enableassertions,或简称–ea,如下所示:

java –ea AssertionDemo

Assertions can be selectively enabled or disabled at class level or package level. The disable switch is –disableassertionsor –dafor short.

可以在类级别或包级别有选择地启用或禁用断言。禁用开关是–disableassertions简称 –da

For example, the following command enables assertions in package package1 and disables assertions in class Class1.

例如,以下命令启用包 package1 中的断言并禁用类 Class1 中的断言。

java –ea:package1 –da:Class1 AssertionDemo

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

不要在公共方法中使用断言进行参数检查。可以传递给公共方法的有效参数被视为方法契约的一部分。无论断言是启用还是禁用,都必须始终遵守合同。例如,上面的代码应该使用异常处理来重写