Skip to content
← All posts
· Delta1 Labs Control flowNebula

How exception handlers hide .NET control flow

A decompiler rebuilds your if/else from the IL branch instructions it finds. Routing a decision through a thrown-and-caught sentinel moves that edge into the exception-handling clause table, where structured decompilation breaks down. Here is the real IL, why it defeats a static read, and the runtime cost that forces you to use it sparingly.

Decompiling a renamed .NET assembly, the thing that still reads cleanly is control flow. Identifiers become a, b, M3; the shape of the logic survives. That is because a decompiler reconstructs C# by reading IL branch instructions — br, brtrue, bge — and matching their pattern back to if, else, while. The branch graph is the readability, which is why obfuscators spend so much effort on it. Flattening and opaque predicates both attack the branch graph directly.

There is a second, quieter place a .NET method keeps its control flow: the exception-handling clause table. Routing a decision through it attacks a part of the decompiler that the branch-graph transforms never touch.

The one place a branch is not a branch

Every method carries a table of protected regions — the structured exception handling (SEH) clauses. Each row is a tuple: the offset and length of a try region, the offset and length of its handler, and what the handler catches. The C# compiler only ever emits these for genuine try/catch/finally, and it emits them well-nested and tidy. Decompilers lean hard on that assumption: they read the clause table, assume it was produced by a compiler, and fold it back into structured C#.

The IL verifier also enforces a rule that matters here. You cannot br or ret your way out of a protected region. The only legal way to leave a try or a catch is the leave instruction, which transfers control to an offset outside the region and, as a side effect, empties the evaluation stack. That stack-clearing is the lever.

Carrying a decision through a catch

Take a method whose whole content is one branch:

static int Classify(int n)
{
    if (n < 0) return -1;
    return 1;
}

Compiled normally, that is a bge (branch-if-greater-or-equal) and two return blocks — a shape any decompiler prints straight back. The exception-edge transform keeps the behaviour but makes the n >= 0 path reachable only by throwing a sentinel type that a handler catches:

.method private hidebysig static int32 Classify(int32 n) cil managed
{
    .locals init (int32 result)
    .try
    {
        ldarg.0
        ldc.i4.0
        bge.s      RAISE          // n >= 0  -> take the exception edge
        ldc.i4.m1
        stloc.0                    // result = -1
        leave.s    DONE
      RAISE:
        newobj     instance void ControlFlow::.ctor()
        throw                      // the only edge into the catch
    }
    catch ControlFlow
    {
        pop                        // discard the sentinel
        ldc.i4.1
        stloc.0                    // result = 1
        leave.s    DONE
    }
  DONE:
    ldloc.0
    ret
}

The logic is unchanged — n < 0 still yields -1, everything else yields 1. But the “else” branch is no longer a branch. It is a throw of ControlFlow, and the code that runs for it lives in a catch handler. The clause table now has a row that says “if an exception escapes offsets X–Y, run the handler at Z” — and the program’s own normal path is what raises that exception.

Before — a branch the decompiler reads directlyn < 0 ?return -1return 1truefalse→After — the false edge goes through the EH tablen < 0 ?result = -1 · leavethrow ControlFlowcatch: result = 1throwhandlerEH clause rowtry X-Y → catch Z
The decision survives, but its false edge is now carried by a thrown sentinel and a catch handler — a row in the exception-handling clause table, not a branch in the instruction stream.

Why a static read breaks down

A correct decompiler has no choice but to be faithful to this. The IL really does throw and really does catch, so the reconstruction it prints is:

static int Classify(int n)
{
    int result;
    try
    {
        if (n >= 0) throw new ControlFlow();
        result = -1;
    }
    catch (ControlFlow)
    {
        result = 1;
    }
    return result;
}

That is behaviourally identical to the original, and far harder to read. The reader has to notice that the throw is unconditional on the taken path, that the catch is the real “else”, and that ControlFlow is a control-flow device rather than an error. Three things compound the pain:

  • The clause table resists structuring. Overlap or nest a few of these regions and the decompiler must represent transfers with gotos and spilled locals, because it can no longer map the regions onto clean nested try blocks. Some decompilers give up and emit unstructured output for the method.
  • leave erases stack state. Because leaving a region clears the evaluation stack, every value that must cross the boundary is spilled to a local first. Expression-oriented decompilers that rebuild a + b * c from stack pushes lose that continuity and fall back to verbose, assignment-by-assignment code.
  • Opaque predicates hide the pruning. The guarding bge can be an opaque predicate — an expression provably constant at runtime but expensive to evaluate statically — so a tool cannot decide the exception edge is “never/always taken” and delete it.

How Nebula.NET applies it

Exception edges are one option in Nebula.NET’s control-flow pass, layered on top of flattening and opaque predicates rather than replacing them. You scope it the same way:

{
  "controlFlowObfuscation": "aggressive",
  "controlFlowExceptionEdges": true,
  "controlFlowInclude": [
    "MyApp.Licensing.*",
    "MyApp.Activation.LicenseGate.Validate"
  ]
}

The sentinel type Nebula injects is itself unnamed and obfuscated, and the engine only rewrites edges where it can prove the transform is sound — it will not convert a branch that crosses a non-empty stack boundary in a way that cannot be spilled legally. As with every transform, correctness is never traded for density: an edge that cannot be moved soundly is left as a branch and hardened another way.

The cost that governs everything

Throwing an exception is expensive — orders of magnitude more than a branch — because the runtime walks the stack to find a handler. This is the single fact that decides how you use the transform. You do not put an exception edge on a hot path; routing a per-iteration decision through a throw will show up in a profiler immediately.

The right target is a decision that runs rarely and matters a lot: the branch inside a licence check, an activation gate, an integrity verdict. A once-per-launch exception costs nothing a user will feel, and in exchange the single most interesting branch in your assembly no longer looks like a branch. That is the trade to weigh — a real runtime cost on a cold path, bought back as a broken decompile on exactly the code you most want to protect.

And the honest ceiling: this raises the cost of reading the control flow, it does not stop an attacker from running the code and watching which path executes. A debugger set on the method still sees n >= 0 decide the outcome. Exception edges are one layer — with opaque predicates on the guard, string encryption on the literals those paths consume, and anti-debug raising the price of dynamic analysis, they remove the static shortcut and force the slow, manual path that obfuscation exists to impose.

Try it on one decision: install the CLI with dotnet tool install --global Delta1Labs.Nebula.Tool, add a sensitive method to your control-flow include list with controlFlowExceptionEdges on, and decompile the before and after. See the Nebula.NET docs to get started, and reference obfuscation for hiding the call graph those methods reach.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.