Return and throw
Place an EXIT node to stop a method early or hand back its result, and place a throw node to stop it with an error.
On this page
Every place a method can stop is a magenta node titled EXIT. Put one on the side of a branch that should give up, and the nodes after it never run on that path.
What it does
Section titled “What it does”An EXIT node ends the method as soon as the white wire reaches it. In a method that hands back a result, the node has a value pin for that result; in a method that hands back nothing, it has only its Exec input.
A throw node, titled THROW, also ends the method, but with an error: Unity stops the work and reports it in the Console, and any catch wrapped around the call gets a chance to handle it. Inside a catch, a RETHROW node passes on the error that catch received.
Because each stop is its own node, a glance at the canvas shows every way out of the method. The four shapes below are CoinsStillNeeded, OpenGoalOnce, SetCoinsToUnlock, and SaveCheckpointOrFail in FlowExamples, a class in the Platformer sample that holds one small method per flow shape so you can open the same diagrams yourself.
When to use it
Section titled “When to use it”Reach for an EXIT node when a method should do nothing in some situations:
- A coin pickup that should stop if the coin is already collected.
- A door that should stop if the player arrives while it is still locked.
- A stomp that should stop if the enemy is already defeated.
Put each of those checks at the start of the method, each with an EXIT node on its stopping side, and the real work sits on one straight white wire after them. Use EXIT with a value at the end of a method that answers a question, such as “can the player jump?”. Use a throw when continuing would leave the game in a broken state, such as a required reference that was never assigned.
What it looks like
Section titled “What it looks like”These are separate example methods, not steps in one edit. Compare the value handed back by EXIT with the exception passed on by RETHROW. The early-return and new-exception examples are paired with C# in What it writes.
How to use it
Section titled “How to use it”Start in a method whose canvas holds only its magenta entry node, the node titled with the method’s name that has an Exec pin and a self pin. To compare each step with a finished diagram, open FlowExamples in the Platformer sample beside it; the steps name the example that shows each piece.
Build a stop, a chained check, and a throw
Place the check
Place the check first. Right-click empty canvas, pick branch under flow control, and wire it from the entry node’s Exec output. Wire the question into its red Condition pin, as described in Branch.
18 stepsFull size ↗Place EXIT
Right-click empty canvas beside the branch and pick return under flow control, or type
returnin search and pick the return row. A magenta node titled EXIT lands where you clicked.
5 stepsFull size ↗Wire the stopping side
Drag from the branch output that means “stop”, often true, to the EXIT node’s Exec input. A white wire joins them.
5 stepsFull size ↗Wire the continuing side
Wire the real work from the branch’s other output.
6 stepsFull size ↗Chain another check
For more reasons to stop, chain another branch on that continuing wire and give it its own EXIT node.
18 stepsFull size ↗Return a value
In a method that returns a value, place an EXIT node at the end of the work too, and fill its value pin: drag a wire into it from a pill or a node’s result, or type a value straight into the pin when it offers a text box or a checkbox.
10 stepsFull size ↗Place THROW
To stop with an error instead, place throw under exceptions, then click the new THROW node.
6 stepsFull size ↗Place and name the exception
Place Exception under exceptions. Click the node it creates, open the Type list in the right rail’s Exception section, and pick the kind of error, such as System.InvalidOperationException. Drag from its output to the throw node’s Exception pin.
14 stepsFull size ↗Wire the failing side
Wire the throw node from the path that should fail, just as you wired the EXIT node in step 3.
5 stepsFull size ↗Compile
Press Compile, beside Revert in the Previewing your changes notice at the top right of the window.
What it writes
Section titled “What it writes”If you also work in C#, this is what lands in the script. FlowExamples.OpenGoalOnce and FlowExamples.SetCoinsToUnlock are written as:
An EXIT node writes return, plus the value on its value pin when it has one. Compile writes it on the line after the node it is wired from, so an EXIT wired after a statement inside an existing if lands inside that if. A chain of new set, return, or throw nodes wired from the entry node is written before the first statement it leads into, or at the end, and that works in a method added in the same Compile. A throw node writes throw plus the exception on its Exception pin. A bare throw; inside a catch rethrows the error that catch received, and a throw can also sit inside a value, such as input ?? throw new ArgumentNullException(nameof(input)).
When it goes wrong
Section titled “When it goes wrong”When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| The EXIT node has no value pin. | Click empty canvas and read the method's return type in the identity header at the top of the right rail. | A method that returns nothing has no value to hand back. Add an output in the right rail's Outputs section if the method should return one. |
| Work after a check never runs. | Follow the white wire from the branch. The EXIT node may be on the output that should continue. | Drag the wire to the EXIT node from the other output, and wire the work from the one that should continue. |
| Compile refuses a throw outside a catch. | Click the throw node. The Throw section reads not set when nothing is wired into exception, and a throw with nothing wired only works inside a catch, where it passes on the caught error. | Place an exception node, pick its type, and wire it into the exception pin, or move the throw onto a catch output. |
- Branch: build the check that leads to an EXIT node.
- Try, catch, and using: catch an error a throw node raises.
- Guard clause: build the early-stop shape step by step.



