Working With Assertions

See Also 

An assertion is an expression that you can place into your code in order to test certain assumptions about your application. Each assertion contains a boolean expression that you assume is true. If the boolean expression is false, the system alerts you by throwing an error during execution.

You can use assertions only if you are running version 1.4 of the J2SE platform. For more information about working with assertions, see:

http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html

Enabling Assertion Parsing in the Source Editor

If you are writing code that contains assertions, make sure the IDE is set to parse Java sources against JDK 1.4. If you do not correctly configure the parser, all assertion expressions will be marked in the Source Editor as errors.

To enable parsing of JDK 1.4 source:

  1. From the main window, choose Tools and choose Options.
  2. Select the Java Sources node and set the Enable JDK 1.4 Source property to True. This property is set to True by default.

Enabling Assertions for Compiler Types

You can compile code that contains assertions by passing the -source 1.4 switch to the compilers. Code that is compiled with the -source 1.4 switch only works when executed on JDK 1.4.

To enable assertions for a compiler type:

  1. Choose Tools and choose Options from the main window.
  2. Expand the Building node and select the node for the appropriate compiler type.
  3. Set the Enable JDK 1.4 Source property to True.

Enabling Assertion Checking for Executors

Assertion checking is available for the external executor and the debugger executor. Additional executors with assertion support might be installed in your version of the IDE by extension modules.

To enable assertion checking for the external executor:

  1. From the main window, choose Tools and choose Options.
  2. In the Options window, expand Debugging and Executing and expand Execution Types.
  3. Select the External Execution node.
  4. Set the Enable Asserts property to True.

To enable assertion checking for all other executors:

  1. From the main window, choose Tools and choose Options.
  2. In the Options window, expand Debugging and Executing and expand Execution Types.
  3. Select the desired Execution node.
  4. Click the ellipsis (...) button for the External Process property.
  5. In the Arguments field, insert -ea in front of the list of command-line switches.

Enabling Assertions for Debugger Types

You can debug code that contains assertions both with and without assertion evaluation. Assertion evaluation is available for the default debugger and applet debugger. Other debuggers installed by extension modules may also support debugging code that contains assertions. By default, assertion evaluation is disabled for all debugger types.

To enable assertion evaluation for a debugger type:

  1. Choose Tools and choose Options from the main window.
  2. Expand Debugging and Executing and expand Debugger Types and select the node for the appropriate debugger type.
  3. Set the Enable Asserts property to True.
See Also
Switching JDK Versions

Legal Notices