0

以下代码正常工作:

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 提出

为什么异常未被捕获?我应该如何修改我的代码以获得期望的行为?

4

1 回答 1

1

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

fun some n = a n handle exc => 0
and some2 n = b n handle exc => 0
于 2013-11-02T19:25:56.830 回答