problem on custom rule (fxcop v1.32)
Hi,
I use this method:
public override ProblemCollection Check(Member member) {
Method method1 = member as Method;
return null;
}
With this example, there is a problem:
public class Class1 {
public void myMethod() {
object obj;
try
{
obj= new object();
obj = 10;
}
catch
{
obj = 10;
}
finally
{
obj = null;
}
}
}
When i look method1.Instructions, the values aren't good:
method1.Instructions[0].Opcode = _Locals
method1.Instructions[1].Opcode = _Try
method1.Instructions[2].Opcode = _Try
method1.Instructions[3].Opcode = NewObj
The try block appears twice then there is only one in my example.
Have you some explanations.
Thanks
[775 byte] By [
bertrand] at [2007-12-21]
The problem doesn't come from FxCop. When i use ILDASM on myMethod(), there is two try block:
.method public hidebysig instance void myMethod() cil managed
{
// Code size 33 (0x21)
.maxstack 1
.locals init ([0] object obj)
.try
{
.try
{
IL_0000: newobj instance void [mscorlib]System.Object::.ctor()
IL_0005: stloc.0
IL_0006: ldc.i4.s 10
IL_0008: box [mscorlib]System.Int32
IL_000d: stloc.0
IL_000e: leave.s IL_001b
} // end .try
catch [mscorlib]System.Object
{
IL_0010: pop
IL_0011: ldc.i4.s 10
IL_0013: box [mscorlib]System.Int32
IL_0018: stloc.0
IL_0019: leave.s IL_001b
} // end handler
IL_001b: leave.s IL_0020
} // end .try
finally
{
IL_001d: ldnull
IL_001e: stloc.0
IL_001f: endfinally
} // end handler
IL_0020: ret
} // end of method Class1::myMethod
Why ?
How can i count the real number of try block and not the try block generated by the compiler ?
Thanks
Hi Bertrand,
this is by design, as the scope of the finally block includes the catch block. If something goes wrong in the catch,the finally will still execute. e.g. look at the following sample:
| |
public static void Main() { try { Console.WriteLine("Try"); throw new Exception(); } catch { Console.WriteLine("Catch"); throw new Exception(); } finally { Console.WriteLine("Finally"); } }
|
this will print the following messages:
Try
Catch
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was
thrown.
at Class1.Main()
Finally
Regards,
Jeffrey