2

I have an FSX script which includes a type provider. When the script is run, I would like to show a user-friendly message when the type provider throws an exception. (For example, when the SqlDataProvider cannot connect to the database.)

Is that possible?

This obviously won't work, but illustrates the idea:

type Sql =
    try
        SqlDataProvider< DatabaseVendor = databaseVendor, ConnectionString = connectionString, TableNames = tableNames >
    with ex ->
        printfn "Cannot connect to the database."

Here is a gist that can be used to test potential solutions.

1 Answer 1

1

When the script runs, the exception will be thrown, but not until you use or instantiate the type from the Type Provider.

In your case, you wouldn't wrap the definition of the type in a try, but rather the usage:

type Sql = SqlDataProvider<...>

// When you go to _use_ the type, handle exceptions:
try
    let ctx = Sql.GetDataContext ()
    // .. use
with
| :? SomeExceptionType as ex -> printfn "Cannot connect to the database."
3
  • When I run the code in this gist with "Microsoft (R) F# Interactive version 10.6.0.0 for F# 4.7", exception handling is not run... gist.github.com/WallaceKelly/de7b741f32eb6aad192c989d04b3c20f Jan 14, 2020 at 16:36
  • @WallaceKelly Are you actually using the context? I think the exception throws at runtime when the context access actually occurs. Jan 14, 2020 at 17:52
  • I was not originally using it. However, I just updated the gist and re-tested. In both cases, the script fails with a compiler error FS3033, never giveing the exception handling a chance to run. Jan 14, 2020 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.