Can I somehow make my objects, without exception, become “castable”, so that any local variables declared in a try block, for example obj , in a try block below
try { SomeObject obj = new SomeObject(); }catch {}
can cross the border of the scope in the catch {} field. I want to use the obj instance in the catch {} clause, even if its state is compromised. Is it possible somehow?
Perhaps it is possible (?), Since the C # compiler probably does the same (for optimization, which I would suggest?), And allows the catch clause to get any System.Object, but I cannot use it in VS, for this method :
public void Foo() { try { } catch(Exception ex) { } try { } catch { } }
generates this IL:
.method public hidebysig instance void Foo() cil managed { // Code size 22 (0x16) .maxstack 1 .locals init ([0] class [mscorlib]System.Exception ex) IL_0000: nop .try { IL_0001: nop IL_0002: nop IL_0003: leave.s IL_000a } // end .try catch [mscorlib]System.Exception { IL_0005: stloc.0 IL_0006: nop IL_0007: nop IL_0008: leave.s IL_000a } // end handler IL_000a: nop .try { IL_000b: nop IL_000c: nop IL_000d: leave.s IL_0014 } // end .try catch [mscorlib]System.Object /* <-- I want to do like this*/ { IL_000f: pop IL_0010: nop IL_0011: nop IL_0012: leave.s IL_0014 } // end handler IL_0014: nop IL_0015: ret } // end of method Class::Foo
Is there a way (managed / unchanged) to “spoof” like this and define “rushing” behavior for SomeObject type SomeObject that I can leave my right to inherit from meaningful classes and not inherit from exception classes just make the object “catchy”?
How about if I like it (which pretty much I'm doing it now):
SomeObject obj = null; try { obj = new SomeObject(); }catch {}
works slower if obj is defined inside or outside the try clause, since t looks like they both generate identical local method storage variables (and not the scope of the test) - you get .locals init ([0] class ClassLibrary.Class obj in IL in anyway, so maybe it’s not slower if the variable never arises? In other words, if I have SomeObject obj = null; defined somewhere inside the method code, but I never initialize it in the actual SomeObject instance, does it matter (performance wise) if my definition is va Is ruable located inside the try / catch or global scope of the entire function (not inside try / catch)?