Synopsis A map does not contain a requested key.
Function data RunTimeException = NoSuchKey(value key);
Usage import Exception;
Description Rascal provides many operations and functions on maps, see Rascal:Values/Map and Rascal:Prelude/Map. This error is generated when a function or operation cannot find a requested key value in a map.

Examples Import the Map and IO libraries and introduce map M:
rascal>import Map;
rascal>import IO;
rascal>M = ("a" : 1, "b" : 2);
map[str, int]: ("a":1,"b":2)
Indexing M with a non-existing key gives an error:
|stdin:///|(2,3,<1,2>,<1,5>): NoSuchKey("c")
	at ___SCREEN_INSTANCE___(|stdin:///|(0,23,<1,0>,<1,23>))

Use the postfix isDefined operator ? to test whether the value is defined:
rascal>if(M["c"]?) println("defined"); else println("not defined");
not defined
Or use the binary ifDefinedElse operator ? to return an alternative value when the value of M["c"] is undefined:
rascal>M["c"] ? 3
int: 3
Yet another solution is to use try/catch. First we import the Rascal exceptions (which are also included in Prelude):
rascal>import Exception;
rascal>try println(M["c"]); catch NoSuchKey(k): println("Key <k> does not exist");
Key c does not exist
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