Cant handle SML exceptions with parameters

528 views Asked by At

Lets say I have an exception defined as below:

exception MyException of string

I raise it in a function (this function returns string) as follows:

fun foo ... = raise DomenaInterpretacije ("Error ...")   
  | foo ... ...

Then I call that functon in a way, that produces the exception:

fun testExc () =
     (foo ...)
     handle MyException msg => msg

But these yields:

Error: non-constructor applied to argument in pattern: MyException 
Error: unbound variable or constructor: msg

What am I doing wrong here?

2

There are 2 answers

0
user2340939 On BEST ANSWER

The exception was actually defined inside a module, so I had to call it like MyModule.MyException msg.

0
sshine On

Apparently, what you are doing wrong has little to do with the code you have pasted.

A working example of your code is provided below:

exception MyException of string

fun foo () = raise MyException "I wonder what happened."

fun testFoo () =
    foo ()
    handle MyException msg => msg