Throwing Values
In Coco, all classes have a reserved special method __except__()
to define the string value that must be returned when a class is thrown
throw tl
In this statement tl
is a object of type TooLong()
, when tl is thrown the __except__()
method is called.
A string value can also be thrown, __except__()
is predefined for specific inbuilt types and can be directly thrown.
method
__except__
was called __throw__
prior to cocolang v0.5.1catch statements are not currently supported yet in cocolang v0.5.2
coco Throw
class TooLong:
method __except__() -> (err String):
yield err "string too long"
endpoint invoke TestThrow(s String):
throw s
endpoint invoke TestLen(s String) -> (l U64):
memory l1 = len(s)
memory l2 = s.__len__()
if l2 > 10:
memory tl = TooLong{}
throw tl
if l1 != l2:
memory ts = f"Lengths not equal {l1} != {l2}".__throw__()
throw ts
yield l l1