在由“和”分隔的一组多个函数之后处理异常

时间:2013-11-02 18:44:56

标签: exception exception-handling sml ml

以下代码正常运行:

local 
  exception exc;
  fun a 0 = 0
    | a n = if (n>0) then n+ b (n-1) else raise exc
  and b 0 = 0
    | b n = if (n>0) then n+ a (n-1) else raise exc
in
  fun some n = a n
  handle exc => 0;
end;

some ~4;

val some = fn : int -> int
val it = 0 : int

但是如果我在in .. end部分

中添加以下修改
in
  fun some n = a n
  and some2 n = b n
  handle exc => 0;
end;

并且有同样的电话some ~4;

我得到以下输出:

val some = fn : int -> int
val some2 = fn : int -> int
E:\Program Files\SML_NJ\\bin\.run\run.x86-win32.exe: Fatal error -- Uncaught exception exc with 0

在E:\ GenCS \ SML教程中提出\ some2.sml:181.49-181.52

为什么没有删除例外?我应该如何修改我的代码以获得所希望的行为?

1 个答案:

答案 0 :(得分:1)

handle仅适用于some2函数。如果您希望两个函数都处理异常,

fun some n = a n handle exc => 0
and some2 n = b n handle exc => 0