F # Exception Filters

The article http://blogs.msdn.com/b/dotnet/archive/2009/08/25/the-good-and-the-bad-of-exception-filters.aspx assumes that F # supports exception filters ( e.g. not containing syntax in C #). Exception filters are run before the corresponding catch block, and if they return true, the catch block will be executed. I would suggest that F # does this using something like this

with | ex when filter(ex) -> printfn "Caught" 

However, for me, it compiles to the usual "catch [mscorlib] System.Object" with a call to the filter function inside the catch block, and there is no filter section in the generated MSIL. So the question is, does F # really support this construct?

thanks

+8
exception clr f #
source share
1 answer

As far as I know, F # does not actually implement / does not use / does not open the filter handlers available in MSIL (ECMA-335, 5th ed., Section I, section 12.4.2 "Exception Handling"). According to section 6.9.21 of the F # 3.0 specification, the compiler must compile the whole with clause into a catch ; the "reset" case is added to the compiled code, so if the excluded catch does not match any of the patterns in the with clause, it is re-raised (via the rethrow IL rethrow ).

However, I would really like F # to support lower-level IL / CLR constructs - they are often not used, but sometimes they provide the only way to implement something correctly or avoid the need for complex workarounds; and, as with the OP, it is important that F # supports them to ensure compatibility. For example, try...fault will actually be convenient for logging, and this will simplify some bits of code that try...finally should use with additional logic (for example, the implementation of lock in FSharp.Core ).

UPDATE: I was just looking for information on a completely different topic and came across this post since 2006 on the blog Don: F # 1.1.13 is now available! (also see accompanying release notes ). Of course, F # 1.1.13 was a very early version of the language, and at that time it was quite experimental, but it is interesting to see that the compiler had a switch --generate-filter-blocks .

+4
source share

All Articles