.NET, Architecture, ALM, Cloud
C# Code :
Action<int, int> trySample = (a, b) =>
{
try
{
int result = a / b;
Console.WriteLine("Result : {0}", result);
}
catch (System.DivideByZeroException)
{
Console.WriteLine("cannot divide by zero");
}
catch (Exception)
{
Console.WriteLine("UnexpectedException");
throw;
}
finally
{
Console.WriteLine("bloc finally");
}
};
Expression Trees Code :
var aParam = Expression.Parameter(typeof(int), "a");
var bParam = Expression.Parameter(typeof(int), "b");
var resultVariable = Expression.Variable(typeof(int), "result");
Expression<Action<int, int>> expression =
Expression.Lambda<Action<int, int>>(
//{
Expression.Block(
new[] { resultVariable },
Expression.TryCatchFinally(
//try
Expression.Block(
//int result = a / b;
Expression.Assign(resultVariable, Expression.Divide(aParam, bParam)),
//Console.WriteLine("Result : {0}", result);
Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), resultVariable)),
// finally {Console.WriteLine("bloc finally"); }
Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("bloc finally")),
//catch (System.DivideByZeroException)
Expression.Catch(typeof(DivideByZeroException),
// Console.WriteLine("cannot divide by zero");
Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("cannot divide by zero"))),
//catch (Exception)
Expression.Catch(typeof(Expression),
//{
Expression.Block(
//Console.WriteLine("UnexpectedException");
Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("UnexpectedException")),
//throw;
Expression.Rethrow())))),
aParam, bParam);
Expression Trees Debug View :
.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>(
System.Int32 $a,
System.Int32 $b) {
.Block(System.Int32 $result) {
.Try {
.Block() {
$result = $a / $b;
.Call System.Console.WriteLine($result)
}
} .Catch (System.DivideByZeroException) {
.Call System.Console.WriteLine("cannot divide by zero")
} .Catch (System.Linq.Expressions.Expression) {
.Block() {
.Call System.Console.WriteLine("UnexpectedException");
.Rethrow
}
} .Finally {
.Call System.Console.WriteLine("bloc finally")
}
}
}
IL Code :
//int result = a / b;
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: div
IL_0003: stloc.0
//Console.WriteLine("Result : {0}", result);
IL_0004: ldloc.0
IL_0005: call 6000003 System.Console.WriteLine(Int32)
IL_000a: leave IL_0031
IL_000f: pop
IL_0010: ldstr 70000005 "cannot divide by zero"
IL_0015: call 6000006 System.Console.WriteLine(System.String)
IL_001a: leave IL_0031
IL_001f: pop
//Console.WriteLine("UnexpectedException");
IL_0020: ldstr 70000008 "UnexpectedException"
IL_0025: call 6000009 System.Console.WriteLine(System.String)
//throw;
IL_002a: rethrow
IL_002c: leave IL_0031
IL_0031: leave IL_0041
//Console.WriteLine("bloc finally");
IL_0036: ldstr 7000000a "bloc finally"
IL_003b: call 600000b System.Console.WriteLine(System.String)
IL_0040: endfinally
IL_0041: ret