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 5929057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:22:16+00:00 2026-05-22T14:22:16+00:00

Without taking the effort to actually clone JUnit or something, I’m throwing together a

  • 0

Without taking the effort to actually clone JUnit or something, I’m throwing together a few utility functions to help test some SML code. I do know about QCheck, but it can’t do this one thing either and isn’t what I want generally. (But if you know of another automated-testing framework for SML, please speak up.)

I’d like to be able to assert that some function will throw an exception, e.g., given a function

fun broken x = raise Fail

I’d like to be able to write something like

throws ("ERROR: function is not broken enough!", fn () => broken 1, Fail)

and have it throw an error if the given function does not raise the expected exception.

I tried to write a throws function with type (string * exn * (unit -> unit)) -> unit like so:

  fun throws (msg, e, func) = func ()
    handle e' => if e = e'
                     then ()
                     else raise ERROR (SOME msg)

But this generates a bunch of compile-time errors, apparently because ML
does not define equality over exceptions:

sexp-tests.sml:54.31-57.49 Error: types of rules don't agree [equality type required]
  earlier rule(s): ''Z -> unit
  this rule: exn -> 'Y
  in rule:
    exn => raise exn
sexp-tests.sml:54.31-57.49 Error: handler domain is not exn [equality type required]
  handler domain: ''Z
  in expression:
    func ()
    handle 
        e' => if e = e' then () else raise (ERROR <exp>)
    | exn => raise exn

As a workaround, I suspect I could just reuse an existing assert function I have:

assert ((broken 1; false) handle Fail => true | _ => false)

But it’s a bit more thinking and typing.

So, is there any way to write that throws function in SML?

  • 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-22T14:22:17+00:00Added an answer on May 22, 2026 at 2:22 pm

    This following function should work:

    exception ERROR of string option;
    
    fun throwError msg = raise ERROR (SOME msg);
    
    fun throws (msg, func, e) =
        (func (); throwError msg) handle e' =>
            if exnName e = exnName e'
            then ()
            else raise throwError msg
    

    This uses the function exnName, which gets the name of the exception as a string, and uses that for comparison instead.

    More importantly, it also handles the case where no exception is thrown at all, and gives an error on that, too.

    Alternatively, if you simply need a boolean value, indicating whether the exception was thrown or not, you can use:

    fun bthrows (func, e) = (func (); false) handle e' => exnName e = exnName e'
    

    Note that, for the case of Fail, you’ll actually have to create an instance of a Fail-exception, for instance like so:

    throws ("ERROR: Oh no!", fn () => test 5, Fail "")
    

    Alternatively, you could take the name of the exception, for a cleaner general case:

    fun throws (msg, func, e) =
        (func (); throwError msg) handle e' =>
            if e = exnName e'
            then ()
            else raise throwError msg
    
    fun bthrows (func, e) = (func (); false) handle e' => e = exnName e'
    

    And then use it like this:

    throws ("ERROR: Oh no!", fn () => test 5, "Fail")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've been creating functions for too long without taking my code to 'classes'. I
Is there any way to write something like this without taking over emacs? (defun
Is it possible to somehow do this without taking out the @? It does
I need to do a date comparison in Mysql without taking into account the
I made some changes to an open source project without taking time to create
I have some C# class libraries, that were designed without taking into account things
Is it possible to analyze the images without taking a foto on the iPhone
Is there any downside to calling pthread_cond_timedwait without taking a lock on the associated
When I write something, half the effort tends to go into adding clear and
What is the best practice to backup a lucene index without taking the index

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.