Sublime Text 3 smlnj REPL不打印异常

时间:2018-10-10 16:12:40

标签: exception sublimetext3 sml read-eval-print-loop smlnj

当代码中引发异常时,我什么也没收到。例如下面的代码:

fun test n = 
if n = 1
then raise Fail ("hey")
else 2

val t = test 1

当我键入

- use "test.sml";

输出就是

[opening test.sml]
val it = () : unit

我希望打印出出现了异常和消息(“嘿”)

有关如何解决此问题的任何想法 我正在使用smlnj 110.82

1 个答案:

答案 0 :(得分:2)

因为您没有处理您的例外情况。

您应该喜欢:

fun test n =
if n = 1
then raise Fail ("hey")
else 2

val t = test 1 handle Fail msg => 0;

[opening practice.sml]
val test = fn : int -> int
val t = 0 : int
val it = () : unit

如果您希望在屏幕上看到信息失败,请使用副作用:

fun test n =
if n = 1
then raise Fail ("hey")
else 2

val t = test 1 handle Fail msg => (print (msg ^ "\n"); 0)

[opening practice.sml]
hey
val test = fn : int -> int
val t = 0 : int
val it = () : unit