Wiki
Version 1 (modified by hopscc, 11 years ago)

--

Try-Catch-Get Expression

The try expression provides a simple way to run a single expression that may generate a single exception Type and provide a default value if it does so.

It's a convenience for a try-catch statement for a single statement which would assign the expression.

The value of the expression is the value of the expression following 'try' or if an exception is thrown the value of the expression after the 'get'.

If an Exception-Type is not given it will catch all Exceptions.

Grammar

try <expr> catch [ <Exception-Type> ] get <expr>

Examples

inVal = '..' #an int string presumably
x = try int.parse(inVal) catch FormatException get 0 #  return 0 if inVal not an int String

# Catch all exceptions
dflt = '---'
s1 = try String.format('{1:P}', 0.123) catch get dflt
assert s1 == '---'

name = try .input('Enter your name:') catch ConsoleInputException get 'ERROR'