# Tutorial 11: Errors and try/catch/finally
# Goal: Handle problems gracefully instead of crashing.
#
# throw "message" raises an error.
# try ... catch e ... finally ... end catches it and always runs cleanup.

func divide(a, b)
    if b == 0
        throw "cannot divide by zero"
    end
    return a / b
end

func require_positive(n)
    if n <= 0
        throw "expected a positive number, got " + str(n)
    end
    return n
end

try
    result = divide(10, 0)
    say("result: " + str(result))
catch err
    say("caught: " + err)
finally
    say("cleanup always runs")
end

say("still running after the error")

try
    say("10 / 2 = " + str(divide(10, 2)))
catch err
    say("should not reach here")
end

try
    require_positive(-3)
    say("never printed")
catch err
    say("caught: " + err)
end

# Challenge: wrap an ask() in try/catch to reject empty input.
