↑ Up |
In the following an exception of type E
is thrown an then catched.
class E: Exception = {} try raise table E{text = "Some message"} catch e if e: E print(e.text) end
The catch-part is only reached if the exception is of
type E
. It can also be stated by means of
an unconditional catch.
try raise table E{text = "Some message"} catch e if e: E print(e.text) else raise e end end
But in case of an unconditional catch, the traceback so far will be clipped off.
Destructors are called automatically during stack unwinding. The following program demonstrates the behavior.
class E: Exception = {} class Duck = { function drop() print("A duck flies away.") end } function subroutine() d = table Duck{} raise table E{text = "Error"} end try subroutine() catch e if e: E print(e.text) end
The output is produced always in this order:
A duck flies away. Error
Assertions check at runtime for an invariant that shall stay true each time the program execution passes the specific point. When an assertion is not fulfilled, an exception is thrown. The syntax allows for brief and verbose exception messages:
assert type(x) is Int and x>=0 assert type(x) is Int and x>=0, "x: Int, x>=0" assert type(x) is Int and x>=0, begin "Expected x: Int, x>=0, but got x = {}" % [x] end assert type(x) is Int and x>=0, begin argv = [type(x),x] "Expected x: Int, x>=0, but got x: {}, x = {}" % argv end