Return and throw
Read value returns, early guard exits, throw statements, rethrows, and throw expressions.
On this page
A door method should stop immediately when there is no player, the goal is locked, or the level already finished. Early return nodes make those guard exits visible before the successful path.
The problem
Section titled “The problem”Door.Enter has three reasons to do nothing and one path that completes the level. Nesting the success path under three conditions would push the important work deeper with every rule. The method uses early returns to reject invalid cases first.
When to reach for it
Section titled “When to reach for it”Use a return node at the normal end of a value-returning method or as an early exit. On the canvas a return node is titled EXIT. A return with an expression has a value input pin; return; in a void method has no value pin. Use throw when the method must terminate with an exception. A bare throw in catch rethrows the current exception. A throw expression can occupy supported value positions such as the right side of ??.
See it
Section titled “See it”
-
Open
Door.Enterand follow the first branch. Whenplayer == nullis true, its path reaches a return node and stops. -
Follow the false path to the next guard. When the door is not open, the method logs the locked state and returns.
-
Follow the remaining path to the
IsCompletedguard. Its true path returns without repeating completion work. -
Continue through all three false paths. This is the success spine: set completion, refresh the HUD, play audio, disable the player object, and log the result.
-
To add an exit, place
returnfrom the flow-control actions. In a value-returning method it arrives with avaluepin of the method’s return type; in a void method it has none. Connect the result expression tovalue: a wire supplies the expression, and an eligible unwired pin can hold an inline literal. -
Wire the return from the statement it follows. Compile writes it on the line after that statement, so a return wired after a statement inside an existing
iflands inside thatif. A chain of new set, return, or throw nodes wired from the entry node is written into the body before the first statement it leads into, or at the end, and that works in a method added in the same Compile. -
For an exception path, place
throwfrom the exceptions actions, or inspect an existing throw node, and connect the exception expression. Use a rethrow only inside a catch where the current exception exists.
What it writes
Section titled “What it writes”A value return writes return plus the expression on its value pin. A throw statement writes throw plus its exception expression. In a supported value position, a throw expression keeps forms such as input ?? throw new ArgumentNullException(nameof(input)) intact.
When it goes wrong
Section titled “When it goes wrong”When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| A return node has no value pin. | Read the active method return type and whether the source says return with no expression. | Use a value return only in a method whose signature returns a value. |
| Work after a guard never runs. | Trace which branch output reaches the return node. | Put the return on the rejecting path and keep the success spine on the other output. |
| A bare throw is refused outside catch. | A rethrow needs a current caught exception. | Move it inside the intended catch, or throw an explicit exception expression. |
- Branch: shape the condition that leads to an early exit.
- Try, catch, and using: follow throw and rethrow through handlers.
- Guard clause: practice the Door.Enter shape.