我们的教授要求我们做这在分配中:如何引发打印消息并返回值的异常?
If the threshold given is negative, you should print the message “Error: Negative Threshold” and return an empty list. To do this, define an exception called ThresholdOutOfRange, raise it if the threshold is negative, and handle the exception to achieve the proper behavior.
我不知道如何抛出一个异常,返回值,并打印错误消息。现在我的筹集异常代码是(刚刚与异常的重要位):
fun getnearbylist(center, threshold, ziplist) =
let
exception ThresholdOutOfRange;
fun test_threshold(threshold, zip, nil) =nil
| test_threshold(threshold, zip, ziplist as x::xs) =
if (threshold <0.0) then raise ThresholdOutOfRange
(* [...skipped a long unrelated middle bit. most important is just knowing
this function returns a string list...] *)
else x::test_threshold(threshold, zip, xs)
in
test_threshold(threshold, center, ziplist)
handle
ThresholdOutOfRange => []
end
因此,当引发异常我的代码将只返回一个空列表。鉴于异常必须具有与我所知道的函数相同的返回类型,我该如何才能返回空列表并输出错误消息?
我相信这也被称为'副effecting' – eazar001 2013-04-09 23:55:12