System.TypeInitializationException in a match constructor

时间:2015-10-30 21:33:25

标签: exception-handling f# typeinitializeexception

I am trying to get a Discriminated Union type from a string in F# (4.0, VS2015). My code looks like this:

type FooOrBar =Foo | Bar
let FooOrBarFromString str =
    match str with 
    | "Foo" -> Foo
    | "Bar" -> Bar
    | _ -> raise (System.ArgumentException("Invalid input"))

The problem is, if the default condition is triggered, and the exception is raised, the Argument Exception is wrapped in a System.TypeInitializationException. Ex:

[<EntryPoint>]
let main argv = 
    let result = FooOrBarFromString "baz"

An unhandled exception of type 'System.TypeInitializationException' occurred in Project.exe

Further complicating things, the VS2015 debugger then chokes and shows "Source Not Available". Only by inspecting the inner exception in the locals view can I see what is going on.

Is there a better way of setting this up so that errors in input data don't make debugging a massive pain?

0 个答案:

没有答案