The following code works properly:

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

But if I add the following modification inside the in .. end part

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

And have the same call some ~4;

I get the following output:

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

raised at E:\GenCS\SML tutorial\some2.sml:181.49-181.52

Why is the Exception uncaught? How should I modify my code to get the wished behavior?

有帮助吗?

解决方案

The handle only applies to the function some2. If you want both functions to handle exceptions,

fun some n = a n handle exc => 0
and some2 n = b n handle exc => 0
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top