Reputation: 11309
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.
Upvotes: 0
Views: 199
Reputation: 90180
A better performing solution (that does not throw exceptions) is:
boolean assertionsEnabled = false;
assert (assertionsEnabled = true);
Upvotes: 1
Reputation: 81492
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");
}
Upvotes: 2
Reputation: 3150
try {
assert false;
System.out.println("Assertions disabled.");
}
catch(AssertionError ae) {
System.out.println("Assertions enabled.");
}
Upvotes: 2