Handling and printing exceptions with SML

288 views Asked by At

I have a code that looks quite like:

ignore
  (f ())
  handle
    AssertionError msg         => (print ("assertion error: " ^ msg ^ "\n"); ())
    | _ (* other exceptions *) => (print ("exception raised\n"); ())

But I need to print the generic exception message (with exnMessage?).

How do I catch the _ exception in order to get and print its message?

1

There are 1 answers

1
molbdnilo On BEST ANSWER

Match the exception with a name instead of _ and use exnMessage:

- (hd ([]: string list)) handle e => exnMessage e;
val it = "Empty" : string