Exception Handling

Exception handling is implemented in the CLR. Each managed language exposes exception handling in a language-specific manner. In C#, there are the try, catch, throw, and finally keywords. The language compiler, such as csc, compiles code for exception handling into language-agnostic MSIL code. For the details of exception handling in C#, read Chapter 12.

There are two strategies for implementing exception handling in MSIL code: as an exception clause or as scoped exceptions.

Exception clauses are found after the core MSIL code of a method in the exception-handling section. An exception clause identifies the protected block, the exception filter, and the exception handler. Execution must fall into a protected block naturally. To exit the protected block or handler, use the leave instruction. Of course, when an exception is raised, execution is transferred away from the protected block. You should fall out of a protected block or exception handler. Do not attempt to exit a protected block or exception handler with a branch statement.

Here is the syntax of the try clause:

.try label1 to label2 exceptiontype handler label3 to label4

label1 and label2 define the protected code, whereas label3 and label4 define the range of the handler. The exceptiontype element specifies the type of handler as catch, filter, finally, or fault handler. Fault handlers are not directly available in C#.

In the previous example, an overflow exception is thrown and not caught. The following code catches the overflow exception:

.assembly extern mscorlib {}
.assembly application {}

.namespace Donis.CSharpBook {

    .class Starter {

        .method static public void Main() il managed {
            .entrypoint
start:      ldc.i8 4000000000
            conv.ovf.i4
            pop
            leave done
stop:       callvirt instance string [mscorlib] System.Exception::get_Message()
            call void [mscorlib] System.Console::WriteLine(string)
            leave done
done:       ret
            .try start to stop catch [mscorlib] System.Exception
                 handler stop to done
        }
    }
}

Scoped exceptions are similar to exception handling in C#, in which there is a try block and a catch block. Scoped exceptions do not employ exception clauses at the end of the method.

In the following sample code, the conversion exception code is handled with a scoped exception handler:

.assembly extern mscorlib {}
.assembly application {}

.namespace Donis.CSharpBook {

    .class Starter {

        .method static public void Main() il managed {
            .entrypoint
            .try {
                ldc.i8 4000000000
                conv.ovf.i4
                pop
                leave done
            }
            catch [mscorlib] System.Exception {
                callvirt instance string [mscorlib] System.Exception::get_Message()
                call void [mscorlib] System.Console::WriteLine(string)
                leave done
            }
done:       ret
        }
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.181.146