Exception 如何在SML/NJ中报告未处理的异常?

Exception 如何在SML/NJ中报告未处理的异常?,exception,handle,smlnj,raise,Exception,Handle,Smlnj,Raise,我在一个名为testexc.SML的文件中有以下SML程序: structure TestExc : sig val main : (string * string list -> int) end = struct exception OhNoes; fun main(prog_name, args) = ( raise OhNoes ) end 我用smlnj-110.74构建

我在一个名为testexc.SML的文件中有以下SML程序:

structure TestExc : sig
               val main : (string * string list -> int)
               end =
  struct

  exception OhNoes;

     fun main(prog_name, args) = (
       raise OhNoes
     )

end
我用smlnj-110.74构建它,如下所示:

ml-build sources.cm TestExc.main testimg
其中sources.cm包含:

Group is
  csx.sml
我这样调用程序(在Mac OS 10.8上):

我希望在调用程序时看到一些东西,但我得到的唯一结果是返回代码1:

$ sml @SMLload testimg.x86-darwin
$ echo $?
1

有什么好处?为什么SML会在这个未处理的异常上无声地失败?这种行为正常吗?是否有一些通用处理程序可以放在main上,用于打印发生的错误?我意识到我可以匹配异常OHNOE,但是对于带有我可能不知道的异常的大型程序呢

答案是处理异常,将其称为e,并使用系统中可用的两个功能打印数据:

$ sml
Standard ML of New Jersey v110.74 [built: Tue Jan 31 16:23:10 2012]
- exnName;
val it = fn : exn -> string
- exnMessage;
val it = fn : exn -> string
-
现在,我们有了修改后的程序,我们将通用处理程序附加到main()上:

我使用列表生成消息,因此要构建此程序,您需要参考sources.cm中的basis库:

Group is
  $/basis.cm
  sources.cm
下面是我们运行它时的样子:

$ sml @SMLload testimg.x86-darwin
Grasshopper disassemble: [OhNoes OhNoes (more info unavailable: ExnInfoHook not initialized)]
$ echo $?
42
我不知道Exninfook是什么,但我至少看到了OhNoes。很遗憾,SML编译器没有为我们添加一个基本的处理程序,以便在编译程序中出现未处理的异常时打印一些内容。我怀疑ml build将负责这项任务

Group is
  $/basis.cm
  sources.cm
$ sml @SMLload testimg.x86-darwin
Grasshopper disassemble: [OhNoes OhNoes (more info unavailable: ExnInfoHook not initialized)]
$ echo $?
42