30 lines
811 B
Mojo
30 lines
811 B
Mojo
# probe: raising through calls, catching, and finally
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: checked 42
|
|
# expect-stdout: age 42
|
|
# expect-stdout: checked -3
|
|
# expect-stdout: bad: negative age
|
|
# expect-stdout: checked x
|
|
# expect-stdout: bad: String is not convertible to integer with base 10: 'x'
|
|
def parse_age(s: String) raises -> Int:
|
|
var n = atol(s)
|
|
if n < 0:
|
|
raise Error("negative age")
|
|
return n
|
|
|
|
|
|
def describe(s: String) -> String:
|
|
try:
|
|
var n = parse_age(s)
|
|
return "age " + String(n)
|
|
except e:
|
|
return "bad: " + String(e)
|
|
finally:
|
|
print("checked", s)
|
|
|
|
|
|
def main():
|
|
print(describe("42"))
|
|
print(describe("-3"))
|
|
print(describe("x"))
|