using System; namespace ConsoleApplication1 { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { object objTest1 = null; try { int intTest1 = (int) objTest1; } catch (NullReferenceException e) { Console.WriteLine(e.Message); } } } }
.namespace ConsoleApplication1 { .class /*02000002*/ private auto ansi beforefieldinit Class1 extends [mscorlib/* 23000001 */]System.Object/* 01000001 */ { .method /*06000001*/ private hidebysig static void Main(string[] args) cil managed { .entrypoint .custom /*0C000001:0A00000E*/ instance void [mscorlib/* 23000001 */]System.STAThreadAttribute/* 0100000F */::.ctor() /* 0A00000E */ = ( 01 00 00 00 ) // Code size 27 (0x1b) .maxstack 2 .locals /*11000001*/ init ([0] object objTest1, [1] int32 intTest1, [2] class [mscorlib/* 23000001 */]System.NullReferenceException/* 01000013 */ e) IL_0000: ldnull IL_0001: stloc.0 IL_0002: ldloc.0 IL_0003: unbox [mscorlib/* 23000001 */]System.Int32/* 01000010 */ IL_0008: ldind.i4 IL_0009: stloc.1 IL_000a: leave.s IL_001a IL_000c: stloc.2 IL_000d: ldloc.2 IL_000e: callvirt instance string [mscorlib/* 23000001 */]System.Exception/* 01000011 */::get_Message() /* 0A00000F */ IL_0013: call void [mscorlib/* 23000001 */]System.Console/* 01000012 */::WriteLine(string) /* 0A000010 */ IL_0018: leave.s IL_001a IL_001a: ret IL_001b: // Exception count 1 .try IL_0002 to IL_000c catch [mscorlib/* 23000001 */]System.NullReferenceException/* 01000013 */ handler IL_000c to IL_001a } // end of method Class1::Main .method /*06000002*/ public hidebysig specialname rtspecialname instance void .ctor() cil managed { // Code size 7 (0x7) .maxstack 1 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib/* 23000001 */]System.Object/* 01000001 */::.ctor() /* 0A000011 */ IL_0006: ret } // end of method Class1::.ctor } // end of class Class1I think this illustrates how easy the CLR makes exception handling wether working with unmanaged/managed, however, I don't think all scenario's have the same runtime cost associated with them.