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

  • SEARCH
  • Home
  • 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 8900447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:06:04+00:00 2026-06-15T01:06:04+00:00

I am trying to write my first generic function in F#, and it looks

  • 0

I am trying to write my first generic function in F#, and it looks like I am missing some concept here :

module Assert =
    let ThrowsException<'e : exn>  functionUnderTest  =
    try 
        let result = functionUnderTest
        printfn "fails"
    with 
    | :? 'e  ->  printfn  "succeeds"
    | _  -> printfn "fails"

stdin(18,28): error FS0010: Unexpected symbol ‘:’ in pattern. Expected ‘>’ or other token.

I would like to be able to use my function like that to test if an exception was raised.

let myFunction=HttpClient.postDocRaw "http://notexisting.com/post.php" "hello=data"
Assert.ThrowsException System.Net.WebException myFunction

I would like to achieve this tool for testing so if anybody has a solution , that’s nice, but furthermore, I am learning F#, and I would like to know where I go wrong trying to write functions like this.

final code :

module Assert =
    let ThrowsException<'e when 'e :> exn>  functionUnderTest  =
        try 
           let actual = functionUnderTest()
           printfn "succeeds"
        with 
        | :? 'e  ->  printfn  "succeeds"
        | _  ->  Assert.Fail()


[<TestClass>]
type HttpClientTest() = 
[<TestMethod>]
    member x.PostDataWrongUrl() = 
        Assert.ThrowsException<System.Net.WebException>  (fun() ->  HttpClient.postDocRaw "http://notexisting.com/post.php" "hello=data")
  • 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-06-15T01:06:05+00:00Added an answer on June 15, 2026 at 1:06 am

    The error says “Expected ‘>'” because a type constraint takes the form 'T when 'T :> baseType. Your code should be:

    module Assert =
      let ThrowsException<'e when 'e :> exn> functionUnderTest =
        try 
          let result = functionUnderTest
          printfn "fails"
        with 
          | :? 'e -> printfn  "succeeds"
          | _  -> printfn "fails"
    

    See Constraints on MSDN for more info.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write my first jquery plugin but I'm running into some difficulties.
I am trying to write some of my first code in emacs lisp and
I am trying to write a recursive C# function that will operate on generic
Trying to write my first generic class in C#: public class HighScoreList<ScoreType> where ScoreType
Just trying to write my first ever WP7 app. I have several hundreds of
I'm trying to write my first simple mvc app. I have a Main View
I'm trying to write my first iterator class / container type. Basically, I want
I am trying to write my first WCF project. I have created a project
I'm trying to write my first iPhone app, and I'm using a date picker
Currently I'm trying to write my first Python library and I've encountered the following

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.