Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4125782
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:54:53+00:00 2026-05-20T23:54:53+00:00

I have an F# exception that it is not being caught in the correct

  • 0

I have an F# exception that it is not being caught in the correct catch block.

Here’s the relevant code:

exception ConfigFileVersionIncompatabilityException of string

[<XmlType("config")>]
type Configuration() = class

    let thisVersion : string = "1.0"
    let mutable fileVersion : string = thisVersion

    [<XmlAttribute("version")>]
    member x.FileVersion
        with get() = fileVersion
        and set v = if v <> thisVersion
                    then raise (ConfigFileVersionIncompatabilityException(String.Format("Was expecting version {0} but read version {1}.", thisVersion, v)))
end


module FilterFileFunctions =

    let sampleConfigFilename = "sample.filters"

    let readConfig (file : string) =
        try
            use xmlDoc = new StreamReader(file) in
                let s = XmlSerializer(typeof<Configuration>)
                s.Deserialize(xmlDoc) :?> Configuration |> Success
        with
        | ConfigFileVersionIncompatabilityException(s) ->
            String.Format("Failed to read the configuration file: \"{0}\";\nThe following reason was given:\n{1}", file, s)
            |> Failure
        | ex ->
            String.Format("Failed to read the configuration file: \"{0}\";\n{1}", file, ex)
            |> Failure

The problem is that the ex catch block catches the ConfigFileVersionIncompatabilityException exception, where it should be caught by the first block.

I tried to use :? System.Exception as ex instead of just ex and it still behaved the same.

Am I missing something?

[Edited 1 minute after initial post to remove irrelevant code.]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:54:53+00:00Added an answer on May 20, 2026 at 11:54 pm

    When an exception occurs during the deserialization, the Deserialize method will catch it and wrap it inside InvalidOperationException. This means that you need to chatch InvalidOperationException and then analyze the InnerException property to get to your user-defined exception.

    try // ..
    with 
    | :? InvalidOperationException as invOp ->
       match inv.InnerException with 
       | :? ConfigFileVersionIncompatabilityException as e -> 
         printfn "%s" e.Data0
       | _ -> // generic handler
    | e -> // generic handler
    

    The Data0 property exposes the value carried by the exception (I used it, because you cannot access it easily in the pattern matching when using :?). However, you can avoid the ugly nesting of match expressions (and the duplication of generic handlers) using active patterns:

    let (|InnerException|) (e:exn) =
        e.InnerException
    
    try // ..
    with 
    | InnerException(ConfigFileVersionIncompatabilityException s) -> 
       printfn "%s" s
    | _ -> // generic handler
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with writing a catch clause for an exception that is
I have an exception being thrown from a C# method, that takes a generic
Ok, I have a strange exception thrown from my code that's been bothering me
I have this code that throws a math domain error exception: v = -1.0
I have a problem where I have an exception being thrown that I am
I have some code that will not run if I don't have a breakpoint.
I have exceptions created for every condition that my application does not expect. UserNameNotValidException
I want to throw an exception to say that we have an invalid email
I have an applet that throws this exception when trying to communicate with the
How have you used the Exception.Data property in C# projects that you've worked on?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.