Navigation
Synopsis The Exception datatype used in all Rascal exceptions.
Types
data RuntimeException
     = ArithmeticException(str message)
     | AssertionFailed() 
     | AssertionFailed(str label)
     | EmptyList()
     | EmptyMap() 
     | EmptySet()
     | IllegalArgument()
     | IllegalArgument(value v)
     | IllegalArgument(value v, str message)
     | IndexOutOfBounds(int index)
     | IO(str message)
     | Java(str class, str message)
     | Java(str class, str message, RuntimeException cause)
     | ModuleNotFound(str name)
     | NoSuchAnnotation(str label)
     | NoMainFunction()
     | NoSuchKey(value key)
     | MultipleKey(value key)
     | ParseError(loc location)
     | PathNotFound(loc l)
     | PathNotFound(set[loc] locs)
     | StackOverflow()
     
// Status to be determined:     
     
//   | AccessDenied(loc l)
//   | FileNotFound(str file)
//   | IllegalIdentifier(str name)
//   | SchemeNotSupported(loc l)
//   | HostNotFound(loc l)
     | ImplodeError(str message)
//   | MissingCase(value x)
     | NoSuchElement(value v)
     | PermissionDenied()
     | PermissionDenied(str message)
//   | Subversion(str message)
     | Timeout()
     ;
Usage import Exception;
Description RuntimeException is an AlgebraicDataType that describes the soft exceptions that can be caught by a Rascal program. Since declarations for ADTs are extensible, the user can add new exceptions when needed.

Exception are generated by Throw and can be caught by TryCatch.
Examples Import relevant libraries:
rascal>import Exception;
ok
rascal>import IO;
ok
Define the map weekend and do a subscription with a non-existing key:
rascal>weekend = ("saturday": 1, "sunday": 2);
map[str, int]: ("sunday":2,"saturday":1)
rascal>weekend["monday"];
|stdin:///|(8,8,<1,8>,<1,16>): NoSuchKey("monday")
	at ___SCREEN_INSTANCE___(|stdin:///|(0,18,<1,0>,<1,18>))


Repeat this, but catch the exception. We use variable N to track what happened:
rascal>N = 1;
int: 1
rascal>try {
>>>>>>>   N = weekend["monday"];
>>>>>>>} catch NoSuchKey(v):
>>>>>>>  N = 100;
ok
rascal>println(N);
100
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.