How Glass reads a method's exception regions
A try/catch/finally block leaves almost no trace in the IL instruction stream — the braces compile into offset ranges stored in a separate exception-handler clause table at the end of the method body. This post reads that table clause by clause, shows the four CLR clause kinds (including the fault handler that has no C# keyword), explains why clause order decides which handler wins, and shows how Glass.NET turns raw offset ranges back into nested try/catch/finally.
Open any method with a try block in a decompiler’s IL view and look for the try. You will not find it. There is no “begin try” opcode, no “catch” instruction, nothing in the instruction stream that says where a protected region starts or stops. The braces you wrote compiled into something else entirely: a small table of byte offsets bolted onto the end of the method body. Reconstructing try/catch/finally from that table is one of the quieter things a decompiler does, and it is a good window into how the CLR actually models structured exception handling.
The real question
The IL instruction stream is linear. Exception handling is not — it is a set of regions, some nested, each with a handler that the runtime jumps to on a fault. So where does that structure live, and how does a reader get from a flat list of opcodes plus a table of raw offsets back to the nested try/catch/finally a developer would recognise?
The answer is the exception-handler clause table. Every method that contains any protected region is emitted with a fat method header, whose flags set CorILMethod_MoreSects. After the IL code — aligned to a 4-byte boundary — comes an extra data section, and that section holds the clause table. Each clause is a fixed row of fields, and those rows are the only record of the try you wrote.
One clause is five numbers and a kind
Take an ordinary method with a catch and a finally:
static int ReadCount(string path)
{
int n = 0;
try
{
n = int.Parse(File.ReadAllText(path));
}
catch (FormatException)
{
n = -1;
}
finally
{
Console.WriteLine("done");
}
return n;
}
The instructions compile to a flat sequence. The region boundaries are expressed only through leave (the normal way out of a protected region or handler) and endfinally, plus the clause table. Rendered the way Glass shows it in the IL view:
.method private hidebysig static int32 ReadCount(string path) cil managed
{
.locals init (int32 n)
IL_0000: ldc.i4.0
IL_0001: stloc.0
.try
{
.try
{
IL_0002: ldarg.0
IL_0003: call string System.IO.File::ReadAllText(string)
IL_0008: call int32 System.Int32::Parse(string)
IL_000d: stloc.0
IL_000e: leave.s IL_001e
}
catch System.FormatException
{
IL_0010: pop
IL_0011: ldc.i4.m1
IL_0012: stloc.0
IL_0013: leave.s IL_001e
}
}
finally
{
IL_0015: ldstr "done"
IL_001a: call void System.Console::WriteLine(string)
IL_001f: endfinally
}
IL_001e: ldloc.0
IL_001f: ret
}
That nesting is Glass’s output. The input is the clause table, which carries no braces at all — just two rows:
Clause 1 kind=EXCEPTION TryOffset=0x0002 TryLength=0x000e HandlerOffset=0x0010 HandlerLength=0x0005 ClassToken=FormatException
Clause 2 kind=FINALLY TryOffset=0x0002 TryLength=0x0013 HandlerOffset=0x0015 HandlerLength=0x0009 ClassToken=0
Each clause is the same shape: a kind, the protected region as an offset plus a length (Try), the handler region as an offset plus a length (Handler), and one extra field whose meaning depends on the kind — a class token for a typed catch, a filter offset for a filtered catch, and nothing (zero) for finally and fault. That is the whole record. Everything a decompiler prints about exception handling is derived from these numbers.
The four kinds
The kind field is a small flag with exactly four meaningful values, and they are the entire taxonomy of CLR exception handling:
EXCEPTION(0x0000) — a typed catch. The extra field is a class token; the handler runs if the thrown object is assignable to that type. This iscatch (FormatException).FILTER(0x0001) — a catch with awhenclause. The extra field is a filter offset: a block of IL that runs with the exception on the stack and returns 1 (run handler) or 0 (keep searching). This iscatch (E e) when (cond).FINALLY(0x0002) — a handler that runs on every exit from the protected region, normal or exceptional. The handler ends inendfinally. This isfinally.FAULT(0x0004) — a handler that runs only on the exceptional exit. Like a finally that is skipped on the normal path. It also ends inendfinally(there is no separateendfault). C# has no keyword for this.
The fault clause is the interesting one, because you cannot write it. The compiler emits it. When you write an iterator or an async method, the state machine’s generated MoveNext needs cleanup that runs if the body throws but not if it completes normally and suspends — and that is exactly a fault handler. Glass shows it honestly in the IL view as fault { }, and when it reconstructs the higher-level C# it folds the fault back into the try/finally semantics the state machine stands for. Seeing a fault clause in a method you did not expect to have one is usually a sign you are looking at lowered compiler-generated code.
Order and nesting are decided by the table, not the braces
Look again at the two clause rows. Both start their protected region at 0x0002, but their lengths differ: the catch protects 0x0002..0x0010, while the finally protects 0x0002..0x0015 — a range that contains the catch’s handler. That containment is how Glass knows the structure is an inner try/catch wrapped by an outer try/finally, rather than two sibling handlers on the same try. The braces were never stored; the geometry of the offset ranges is.
Order matters just as much. The runtime searches the clause table top to bottom and takes the first clause whose protected region covers the faulting instruction and whose type or filter matches. So the compiler must — and does — emit clauses most-specific-first: a catch (ArgumentNullException) before a catch (ArgumentException), and an inner try’s clauses before the enclosing try’s. If a decompiler reordered them it would change which handler wins. Glass preserves table order and reconstructs the nesting from the ranges, so the catch chain you read back fires in the same sequence the runtime would use.
Reading it from the CLI
You do not need the UI to inspect the clause table. Ask glass export for the method in IL and the regions are rendered with their boundaries intact:
$ glass export ./App.dll --member "M:App.Program.ReadCount(System.String)" --il
The output shows the .try / catch / finally scopes Glass derived from the clause rows, with each leave target and the endfinally in place. Drop --il and you get the reconstructed C#, where the same two clauses are printed back as the nested try/catch/finally. For a method you suspect is compiler-generated — an iterator’s MoveNext, say — the IL view is where a fault { } clause will show up that the C# surface never names.
Caveats and where it gets hard
A few honest limits. The clause table records only the physical regions the compiler emitted, and the compiler is free to share and merge them. One finally can protect several logically distinct blocks; a catch that only rethrows may have been optimised away entirely. So the reconstructed braces reflect what the compiler produced, which is faithful to the IL but not always a character-for-character match to the original source.
Filter clauses (when) carry their condition as a separate region of IL rather than a token, so recovering catch (E e) when (cond) means reading that filter block and its endfilter — a deeper topic in its own right. And fault clauses, by design, have no C# spelling: Glass will either fold them into the try/finally meaning of a state machine or, in hand-written IL that has no C# equivalent at all, show the fault { } literally rather than invent a keyword that does not exist. That is the right call — the IL view should tell you the truth about the clause kind even when C# cannot express it.
Takeaway
A try/catch/finally block almost disappears when it compiles. The instruction stream keeps only leave and endfinally; the real structure moves into an exception-handler clause table at the end of the method body, where each clause is a kind plus two offset ranges and one type-or-filter field. Glass.NET reads that table, maps every range back onto the instructions it already decoded, and nests the clauses by containment and table order to rebuild the exact try/catch/finally — including the fault handler the CLR defines but C# cannot write. It is a small, precise corner of the metadata, and it is why decompiling exceptions well means reading the table, not the opcodes. Grab the free build from the Glass.NET download page and open the IL view on the next method whose try block you need to understand.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.