Inspired by Mohamed Mahmoud’s blog posting How to: Create Interfaces with Static Methods via IL? I wrote another sick example on IL (Intermediate Language) to show you how different is the world behind compilers. Tonight I have enough of exceptions, I want to throw some strings! Let’s do it!
Throwing exception in ILAs a first thing take a look at the following code written in IL. In short this code defines class with two methods. Run() is entry point and it is run automatically when compiled assembly is executed from command line. ThrowSomething() is method that throws exception. This exception is caught and program terminates without any errors. I borrowed this code from Vijay Mukhi’s IL book, Chapter 10 “Exception Handling”. .assembly StringMess {}.class private auto ansi Program extends [mscorlib]System.Object
{
.method public hidebysig static void Run() il managed
{
.entrypoint
.locals (class [mscorlib]System.Exception V_0)
.try
{
call void Program::ThrowSomething()
ldstr "Bye"
call void [mscorlib]System.Console::WriteLine
(class System.String)
leave.s IL_001e
}
catch [mscorlib]System.Exception
{
stloc.0
ldstr "Exception was thrown!"
call void [mscorlib]System.Console::WriteLine
(class System.String)
leave.s IL_001e
} IL_001e: ldstr "Finish"
call void [mscorlib]System.Console::WriteLine
(class System.String)
ret
} .method public hidebysig static void ThrowSomething()
il managed
{
newobj instance void [mscorlib]System.Exception::.ctor()
throw
}
} Read more: Gunnar Peipman's ASP.NET blog
Throwing exception in ILAs a first thing take a look at the following code written in IL. In short this code defines class with two methods. Run() is entry point and it is run automatically when compiled assembly is executed from command line. ThrowSomething() is method that throws exception. This exception is caught and program terminates without any errors. I borrowed this code from Vijay Mukhi’s IL book, Chapter 10 “Exception Handling”. .assembly StringMess {}.class private auto ansi Program extends [mscorlib]System.Object
{
.method public hidebysig static void Run() il managed
{
.entrypoint
.locals (class [mscorlib]System.Exception V_0)
.try
{
call void Program::ThrowSomething()
ldstr "Bye"
call void [mscorlib]System.Console::WriteLine
(class System.String)
leave.s IL_001e
}
catch [mscorlib]System.Exception
{
stloc.0
ldstr "Exception was thrown!"
call void [mscorlib]System.Console::WriteLine
(class System.String)
leave.s IL_001e
} IL_001e: ldstr "Finish"
call void [mscorlib]System.Console::WriteLine
(class System.String)
ret
} .method public hidebysig static void ThrowSomething()
il managed
{
newobj instance void [mscorlib]System.Exception::.ctor()
throw
}
} Read more: Gunnar Peipman's ASP.NET blog