Picat Picat Exception Handling and Debugging 1 — Questions and Answers
Question 1: Which construct catches exceptions in Picat?
- try-catch
- catch(Goal, Error, Handler) (Correct answer)
- exception(Goal, Handler)
- on_error(Goal, Handler)
Correct answer: catch(Goal, Error, Handler)
`catch(Goal, Error, Handler)` executes Goal and runs Handler if a matching exception is raised.
Question 2: How do you raise an exception in Picat?
- raise(Error)
- exception(Error)
- throw(Error) (Correct answer)
- error(Error)
Correct answer: throw(Error)
`throw(Error)` raises an exception carrying the given error term in Picat.
Question 3: What is the standard ISO format for a type error term in Picat?
- type_error(Type, Value)
- error(type_error(Type, Value), _) (Correct answer)
- {type_error, Type, Value}
- throw(type(Type))
Correct answer: error(type_error(Type, Value), _)
ISO standard errors in Picat use `error(type_error(Type, Culprit), Context)` as the thrown term.
Question 4: Which error type is thrown when a predicate requires an instantiated argument but receives a variable?
- unbound_error
- instantiation_error (Correct answer)
- variable_error
- binding_error
Correct answer: instantiation_error
`instantiation_error` is thrown when a predicate receives an insufficiently instantiated (unbound) argument.
Question 5: What happens if no `catch` clause matches a thrown exception in Picat?
- The program silently continues
- A default handler runs
- The exception propagates up the call stack (Correct answer)
- The program halts immediately with exit code 1
Correct answer: The exception propagates up the call stack
An unmatched exception continues propagating up the call stack until a matching catch or the top level is reached.
Question 6: How can you write a catch clause that matches any exception regardless of type in Picat?
- catch(Goal, _, Handler) (Correct answer)
- catch_all(Goal, Handler)
- catch(Goal, *, Handler)
- try(Goal, Handler)
Correct answer: catch(Goal, _, Handler)
Using `_` as the error pattern in `catch/3` acts as a wildcard matching any exception term.
Which construct catches exceptions in Picat?