Exceptions
Exceptions signal failures during execution. When thrown, execution stops immediately and the error is returned to the caller.
Throwing Exceptions
Throw a String
endpoint Withdraw(amount U64):
if amount > balance:
throw "Insufficient balance"
Throw with Class
Classes can implement __except__ for custom error messages:
class Person:
field age U64
method __except__() -> (err String):
err = f"Underage: only {self.age} years"
endpoint VerifyAge(age U64):
memory person = Person{age: age}
if person.age < 18:
throw person // Calls __except__ automatically
Use Cases
Use exceptions for:
- Invariant violations — conditions that should never occur
- Permission checks — unauthorized access attempts
- Validation failures — invalid input data
warning
Don't use exceptions for control flow. They immediately halt execution.
Can I catch exceptions in Coco?
try, catch, and finally are reserved keywords for future use. Exception handling is not yet implemented.
Error Types
When testing, errors appear in the format !<error_type>::<error_message>:
| Error Type | When It Occurs |
|---|---|
builtin.AccessError | Map key doesn't exist, index out of bounds |
builtin.ArithmeticError | Integer overflow/underflow |
builtin.RuntimeError | General runtime errors |
user | From throw "message" |