https://toitlang.org/ logo
Could you explain how to catch exceptions in Toit?...
# general
m
Could you explain how to catch exceptions in Toit? The adopted system is clearly different from that in other languages. If you write in the style of dart, java & etc, then the compilation will fail because of 'catch e:'. How can I handle the error without getting an EXCEPTION error message? main: try: // Attempt to perform a risky operation result := riskyOperation 42 print "Operation successful: $result" catch e: // Handle any exception that occurs print "Caught an exception: $e" finally: // Optional finally block, runs regardless of exception print "Cleanup operations in finally block" riskyOperation value/int -> int: if value > 0: return value * 2 else: // Throw an exception for invalid input throw "Value must be positive" Sorry for the trouble, I figured it out: main : try: result := riskyOperationWrapper (:: riskyOperation 42) print "Operation result-> $result" result = riskyOperationWrapper (:: riskyOperation -42) print "Operation result-> $result" finally: // Optional finally block, runs regardless of exception print "Cleanup operations in finally block" riskyOperationWrapper function/Lambda -> any : e := catch --trace=false : return function.call if e: print "Exception: $e.stringify" return "*** Error ***" riskyOperation value/int -> int : if value > 0: return value * 2 else: // Throw an exception for invalid input throw "Value must be positive ?$value"