| Instantiation | categories Lists, Sets and Instructions Exception Handling |
array |
Exceptions are a useful feature of software development: they are used to describe an exceptional or wrong behavior of a block. Exception can be raised, to signal this behavior and are caught by exception handlers that surround the code where the exceptional behavior happened. Exceptions are CLAIRE objects (a descendent from the class exception) and can contain information in slots. The class exception is an "ephemeral" class, so the list of instances is not kept. In fact, raising an exception e is achieved by creating an instance of the class e. Then, the method close is called: the normal flow of execution is aborted and the control is passed to the previously set dynamic handler. A handler is created with the following instruction :
| try <expression> catch <class> <expression> |
| try 1 / x catch any (printf("1/~A does not exists", x), 0) |
The most common exceptions are errors and there is a standard way to create an error in CLAIRE using the error(s:string, l:listargs) instruction. This instruction creates an error object that will be printed using the string s and the arguments in l, as in a printf statement. Here are a few examples :
| error("stop here") error("the value of price(~S) is ~S !", x, price(x)) |
| try (choice(), // create a new world ...) // performs an update that may cause a contradiction catch contradiction (backtrack(), // return to previous world ...) |
| try (choice(), if x true else (backtrack(), false)) catch contradiction (backtrack(), false) |
| when y := some(y in x.possible | branch(x.r = y)) in x.r := y else contradiction!() |