12

What exactly happens when a Java assertion fails? How does the programmer come to know that an assertion has failed?

Thanks.

Student
  • 4,481
  • 8
  • 27
  • 32

7 Answers7

9

If assertions are enabled in the JVM (via the -ea flag), an AssertionError will be thrown when the assertion fails.

This should not be caught, because if an assertion fails, it basically means one of your assumptions about how the program works is wrong. So you typically find out about an assertion failure when you get an exception stack trace logged with your thread (and possibly whole program) terminating.

Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
4

An assertion will only fail if you enabled assertions in the JVM when it started. You can do that by specifying the parameter -ea in the command line. If you do that, then this block of code will throw an AssertionError when it is executed:

public void whatever() {
   assert false;
}

Assertions should be used to detect programming errors only. If you are validating user input or something on those lines, don't use assertions.

Ravi Wallau
  • 10,416
  • 2
  • 25
  • 34
2

It throws an AssertionError which is a subclass of Error. As an Error generally and as a failed assertion in particular, you likely should not try to catch it since it is telling you there's a significant abnormality in your code and that if you continue you'll probably be in some undefined, unsafe state.

QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66
1

If an assertion fails and assertion are enabled at runtime it will throw an AssertionError.
Usually you use assert statements in JUnit testings, when building your application you are running a test utility that will check for errors and tell you.

Take a look at this: Programming With Assertions

Thomas Jungblut
  • 20,854
  • 6
  • 68
  • 91
  • JUnit asserts are different to ordinary Java asserts. They cause a different exception to be thrown (`AssertionFailedError`) and can't be turned off with a JVM flag. – Simon Nickerson Apr 16 '11 at 17:02
  • You're mixing two things. Java assertions have the form 'assert *boolean expression*' or 'assert *boolean expression* : *non-void expression* ' and are checked by the runtime system when enabled. In a JUnit test, the statements like 'assertEquals(a,b)' are method callins of JUnit classes. – Charlie Martin Apr 16 '11 at 17:03
1

It throws an AssertionError. Howeveer, you have to compile the program with the -ea or -enableassertions flag to have it generate an actual exception

Larry Watanabe
  • 10,126
  • 9
  • 43
  • 46
0

Programmer can write try catch block so if a error has occurred then it can be caught in catch and the programmer can come to know

try
{
  assert false;
}
catch(Exception e)
{
  System.out.println("Error has occured");
}
Nikhil
  • 1
0

It throws an Error. It is just like when you get a NullPointerException, but it's a subclass of java.lang.Error. The name is AssertionError.

It's like a NullPointerException in the sense that you don't have to declare the throws or anything, it just throws it.

assert(false);

is like

throw new AssertionError();

if you run your program with the -ea flag passed to the java program (VM).

Nathan
  • 8,093
  • 8
  • 50
  • 76
noripcord
  • 3,412
  • 5
  • 29
  • 26