Fault keyword in try block

While studying the assembly in the reflector, I came across the fault keyword in a compiler-generated class.

Do any of you know the meaning of this keyword?

FROM#

 private bool MoveNext() { bool flag; try { // [...] } fault { this.Dispose(); } return flag; } 

vb.net

 Private Function MoveNext() As Boolean Dim flag As Boolean Try ' [...] Fault Me.Dispose End Try Return flag End Function 
+7
c # try-catch
source share
2 answers

Do any of you know the meaning of this keyword?

Yes. This is not valid C #, but in IL it is the equivalent of finally , but only if an exception is thrown.

There is no direct correlation in C #, so the decompiler cannot decompile it into native C #. You could imitate him with something like:

 bool success = false; try { ... stuff ... success = true; // This has to occur on all "normal" ways of exiting the // block, including return statements. } finally { if (!success) { Dispose(); } } 

I mention this in my article about the details of the implementation of the iterator block , which looks relevant for your specific example :)

+10
source share

I think try { ...; } fault { ...; } try { ...; } fault { ...; } try { ...; } fault { ...; } should be translated into C # try { ...; } catch { ...; throw; } try { ...; } catch { ...; throw; }

-one
source share

All Articles