Navigation
Synopsis An assertion in the Rascal code is false.
Function data RunTimeException = AssertionFailed() | AssertionFailed(str label);
Usage import Exception;
Description An Rascal:Assert statement can be used to check assumptions during the execution of a Rascal program. This error is generated if an assertion is not true.

Remedies:
  • Modify your code to make the assertion true.
  • Modify your assertion to reflect the current behaviour of your code.
  • Catch the AssertionFailed yourself, see Rascal:TryCatch.
Examples A false assertion gives an error:
rascal>assert 3 > 4;
|stdin:///|(0,13,<1,0>,<1,13>): AssertionFailed()
	at ___SCREEN_INSTANCE___(|main://___SCREEN_INSTANCE___|)


Define a function that only increments positive integers:
rascal>int incrPositive(int n) { assert n > 0; return n + 1; }
int (int): int incrPositive(int);
Calling it with a positive integer is fine:
rascal>incrPositive(3);
int: 4
But a negative argument gives an error:
rascal>incrPositive(-3);
|stdin:///|(26,13,<1,26>,<1,39>): AssertionFailed()
	at incrPositive(|stdin:///|(14,1,<1,14>,<1,15>))
	at ___SCREEN_INSTANCE___(|stdin:///|(0,17,<1,0>,<1,17>))


We can also catch the AssertionFailed error. First import the Rascal exceptions (which are also included in Prelude) and IO:
rascal>import Exception;
ok
rascal>import IO;
ok
rascal>try println(incrPositive(-3)); catch AssertionFailed(): println("incrPositive applied to an not positive argument");
incrPositive applied to an not positive argument
ok
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.