IL Fault

time to read 2 min | 343 words

No, this isn't another post blaming Microsoft, it's about a very interesting post about IL instructions. Apperantly IL has a fault instructions that can be used similary to finally, but only occurs if there is an exception, I could see it being used like this:

Transaction trans;
try
{
   trans = connection.BeginTransaction();
  // business logic
}
fault
{
  trans.RollBack();
}
finally
{
  trans.Commit();
}

One thing I'm not sure of is whatever the finally block is called anyway, or if it's a choice between fault or finally. From the sample chapter of Inside Microsoft .NET IL Assembler, I thinks that the finally block is executed even if a fault hanlder exists and an exception was thrown, so the above code may be invalid, but it is still very cool to know that the framework has this ability.