Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion not working

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,

like image 881
Sam Palmer Avatar asked Oct 23 '11 08:10

Sam Palmer


People also ask

How do you fix assertion errors?

In order to handle the assertion error, we need to declare the assertion statement in the try block and catch the assertion error in the catch block.

Why assert is not working in Java?

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.

What causes assertion error?

If the specified object is an instance of Throwable , it becomes the cause of the newly constructed assertion error.

What is the assertion 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.


2 Answers

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

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.

like image 67
Matt Avatar answered Sep 22 '22 00:09

Matt


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.

like image 28
stivlo Avatar answered Sep 19 '22 00:09

stivlo