Throwing Exceptions
Exceptions in Coco signal failures during endpoint execution. You can throw a plain String, or throw a class instance whose except() special method produces the error message. When an exception is thrown, execution stops immediately and the error is returned to the caller; any subsequent code in the endpoint is skipped. Use exceptions for invariant violations and permission checks rather than control flow.
coco Throw
class Person:
field age U64
method adult() -> (is_adult Bool):
is_adult = self.age >=18
method __except__() -> (err String):
yield err f"underage: only {self.age} years"
// Simplest way to throw an exception is to just throw a string
endpoint TestThrow(s String):
throw s
endpoint invoke AgeVerification(age U64):
memory person = Person{age: age}
if !(is_adult) <- person.adult():
throw person.__except__()
Catching Exceptions
try...catch...
statement is not implemented in Coco 0.7.0 or PISA 0.5.0