What are Assertions? Enabling or Disabling Assertions

J.E.D.I. String input = invalid input; try { if input.equalsinvalid input { throw new HateStringException; } System.out.printlnString accepted.; } catch HateStringException e { System.out.printlnI hate this string: + input + .; } } } Here is the expected output of the code: I hate this string: invalid input.

2.6 Assertions

2.6.1 What are Assertions? Assertions allow the programmer to find out if an assumption was met. For example, a date with a month whose range is not between 1 and 12 should be considered as invalid. The programmer may assert that the month should lie between this range. Although it is possible to use other constructs to simulate the functionaly of assertions, it would be hard to do this in such a way that the assertion feature could be disabled. The nice thing about assertions is that the user has the option to turn it off or on at runtime. Assertions can be considered as an extension of comments wherein the assert statement informs the person reading the code that a particular condition should always be satisfied. With assertions, there is no need to read through each of the comments to find out the assumptions made in the code. Instead, running the program itself will inform you if the assertions made are true or not. In the case that an assertion is not true, an AssertionError will be thrown.

2.6.2 Enabling or Disabling Assertions

Using assertions does not require importing the java.util.assert package. They are ideally used to check the parameters of non-public methods since public methods can directly be accessed by any other classes. It is possible that the authors of these other classes are not aware that they will have assertions enabled. The program may not work properly in this case. For non-public methods, these are generally called by codes written by people who have access to the methods. Thus, they are aware that when running their code, assertion should be enabled. To compile files that use assertions, an extra command line parameter is required as shown below. javac –source 1.4 MyProgram.java If you want to run the program without the assertion feature, just run the program normally. java MyProgram However, if you want to enable assertions, you need to use the –enableassertions or –ea Introduction to Programming II Page 33 J.E.D.I. switch. java –enableassertions MyProgram To enable runtime assertion checking in NetBeans IDE, simply follow these steps: 1. Look for the project name in the Projects panel on the left. Right click on the project name or icon. 2. Select Properties. This is the last option of the drop-down menu. 3. Click on Run from the Categories panel on the left. 4. Enter -ea for VM Options text field. 5. Click on OK button. 6. Compile and run the program as usual. The following screen shots will help you understand the given steps: Figure 2.1: Enabling Assertions with NetBeans – Steps 1 2 Introduction to Programming II Page 34 J.E.D.I. Figure 2.2: Enabling Assertions with NetBeans – Step 3 Introduction to Programming II Page 35 J.E.D.I. Figure 2.3: Enabling Assertions with NetBeans – Step 4 Introduction to Programming II Page 36 J.E.D.I.

2.6.3 Assert Syntax