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
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:
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.
By default, the assertions are disabled at runtime. To enable it, use the switch –enableassertions, or –ea for short, as follows:
java –ea AssertionDemo
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.
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