Inside Microsoft .NET IL Assembler
Scope Form of SEH Clause Declaration
The label form of the EH clause declaration is universal, ubiquitous, and close to the actual representation of the EH clauses in the EH table. The only quality the label form lacks is convenience. In view of that, ILAsm offers an alternative form of EH clause description: a scope form. You’ve already encountered the scope form in Chapter 2, which discussed protecting the code against possible surprises in the unmanaged code being invoked. Just to remind you, here’s what the protected part of the method (from the sample file Simple2.il on the companion CD) looks like:
The scope form can be used only for a limited subset of all possible EH clause configurations: the handler blocks must immediately follow the previous handler block or the guarded block. If the EH clause configuration is different, we must resort to the label form or a mixed form:
The IL Disassembler by default outputs the EH clauses in the scope form—at least those clauses that can be represented in this form. However, we have the option to suppress the scope form and output all EH clauses in their generic label form. But let’s suppose for the sake of convenience that we can shape the code in such a way that the contiguity condition is satisfied, allowing us to use the scope form.
A single guarded block with multiple handlers in scope form will look like this:
.try { // Guarded code
Much more readable, isn’t it? The nested EH configuration shown earlier in Figure 11-1 is easily understandable when written in scope form:
.try { .try { .try { // Guarded code
The filter EH clauses in scope form are subject to the same limitation: the handler block must immediately follow the guarded block. But because in a filter clause the handler block includes first the filter block and then, immediately following it, the actual handler, the scope form of a filter clause looks like this:
.try { // Guarded code
And, of course, we can easily switch between scope form and label form within a single EH clause declaration. The general ILAsm syntax for an EH clause declaration is as follows:
<EH_clause> ::= .try <guarded_block> <EH_type_specific> <handler_block>Where <guarded_block> ::= <label> to <label> <scope> <EH_type_specific> ::= catch <class_ref> filter <label> filter <scope> finally Fault <handler_block> ::= handler <label> to <label> <scope>
The nonterminals <label> and <class_ref> must be familiar by now, and the meaning of <scope> is obvious: “code enclosed in curly braces.”