exceptionsmlmlton

Handling and printing exceptions with SML


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?


Solution

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

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