Skip to main content

module Exception

Usage

import Exception;

Synopsis

Exceptions thrown by the Rascal run-time.

data RuntimeException

data RuntimeException  
= Ambiguity(loc location, str nonterminal, str sentence)
| ArithmeticException(str message)
| AssertionFailed()
| AssertionFailed(str label)
| CallFailed(list[value] arguments)
| DateTimeParsingError(str message)
| DateTimePrintingError(str message)
| EmptyList()
| EmptyMap()
| EmptySet()
| IndexOutOfBounds(int index)
| IllegalArgument()
| IllegalArgument(value v)
| IllegalArgument(value v, str message)
| IllegalTypeArgument(str cls, str message)
| ImplodeError(str message)
| InvalidURI(str uri)
| InvalidUseOfDate(str message)
| InvalidUseOfDateTime(str message)
| InvalidUseOfLocation(str message)
| InvalidUseOfTime(str message)
| IO(str message)
| Java(str class, str message)
| Java(str class, str message, RuntimeException cause)
| JavaException(str class, str message)
| JavaException(str class, str message, RuntimeException cause)
| MalFormedURI(str uri)
| ModuleNotFound(str name)
| MultipleKey(value key, value first, value second)
| NoMainFunction()
| NoParent(loc location)
| NoSuchAnnotation(str label)
| NoSuchElement(value v)
| NoSuchField(str name)
| NoSuchKey(value key)
| NotImplemented(str message)
| ParseError(loc location)
| PathNotFound(loc l)
| PathNotFound(set[loc] locs)
| PermissionDenied()
| PermissionDenied(str message)
| RegExpSyntaxError(str message)
| SchemeNotSupported(loc location)
| StackOverflow()
| Timeout()
| UnavailableInformation()
;

Synopsis

The Exception datatype used in all Rascal exceptions.

Description

Since declarations for ADTs are extensible, the user can add new exceptions when needed.

Exceptions are either generated by the Rascal run-time (e.g., IndexOutOfBounds) or they are generated by a throw. Exceptions can be caught with a try catch.

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"];
|prompt:///|(8,8,<1,8>,<1,16>): NoSuchKey("monday")
at $shell$(|prompt:///|(0,18,<1,0>,<1,18>))
ok

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