Nonlocal Transfer

Nonlocal transfer is transferring control out of the currently executing function, past one or more stack frames, to a handler in an ancestor frame.

Why can't static multi-level exit do this?

Static Multi-level Exit only works within a single function — the label must be lexically visible from the break/goto. Once the exit needs to cross function boundaries (the ancestor isn’t in the same source scope), you need a dynamic mechanism that walks up the call stack at runtime.

Motivating example

Routine h calls g, which calls f. From inside f, we want to return directly to h, terminating g’s activation entirely:

A plain return from f only unwinds one frame. We need to skip g without g’s cooperation.

How it’s implemented

The standard mechanism is exception handling — throwing an exception triggers stack unwinding until a matching handler is found. This is nonlocal transfer under the hood.

uC++ extends this with two flavors:

  • Resumption (_Resume) — handler runs in place of the faulting code, no unwinding
  • Termination (_Throw) — stack is unwound to the handler

For cross-coroutine targets (not just cross-function), see Nonlocal Exception.