Question

I want the first line of my Java program output to print whether assertions are turned on. How do I do this?

Edit: An additional requirement is that the program should not terminate before doing useful work.

Was it helpful?

Solution

try {
  assert false;
  System.out.println("Assertions disabled.");
}
catch(AssertionError ae) {
  System.out.println("Assertions enabled.");
}  

OTHER TIPS

How about this? I don't know Java, but I think this may work:

try {
    assert false;
    System.out.println("assertions are disabled");
} catch (AssertionError e) {
    System.out.println("assertions are enabled");
}

A better performing solution (that does not throw exceptions) is:

boolean assertionsEnabled = false;
assert (assertionsEnabled = true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top