Skip to content

Control-flow exceptions

Inside a block, return exits the method the block was written in, and break exits the method that yielded to the block. Neither just leaves the block (see Return Semantics). The compiler implements this with two exceptions. They are an implementation detail of emitted code, documented here for anyone reading the IL or calling Amethyst methods that take blocks from C#.

Each method that yields to a block gets a compiler-assigned target_id. The yield site catches only the exception carrying its own ID, so nested yields unwind to the right method.

class Amethyst::Runtime::ControlFlowReturnException < Exception
getter target_id: Int
getter value: Any?
end

Raised by return inside a block. value is the value being returned, or nil for a Void method.

class Amethyst::Runtime::ControlFlowBreakException < Exception
getter target_id: Int
getter value: Any?
end

Raised by break inside a block. value is the value passed to break, if any.