Throwing Values
In Coco, all classes have a reserved special method __throw__()
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 __throw__()
method is called.
A string value can also be thrown, __throw__()
is predefined for specific inbuilt types and can be directly thrown.
catch statements are not currently supported yet in cocolang v0.1.2
coco Throw
class TooLong:
method __throw__()->(err String):
yield err "string too long"
endpoint invokable TestThrow(s String):
throw s
endpoint invokable 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