我想在代碼扔 此前後添加捕獲異常的機制,但我不能讓它編譯:未能趕上SML一個例外,由於語法錯誤
這裏的無一例外處理代碼 - 它編譯偉大工程:
fun calc(input : string) : int =
let
val outStr = ref "someString"
val outInt = ref 0
in
(
outStr := replaceRomanDec(input); (* replace roman number with decimal *)
outInt := calcMyRomanExpression(!outStr)
);
(!outInt)
end;
但是,當我試圖把handle
和exception
,在這裏:
fun calc(input : string) : int =
exception CalculatorParser
let
val outStr = ref "someString"
val outInt = ref 0
in
(
outStr := replaceRomanDec(input); (* replace roman number with decimal *)
outInt := calcMyRomanExpression(!outStr);
handle CalculatorParser => -1
);
(!outInt)
end;
我得到:
stdIn:1761.2-1761.28 Error: syntax error: deleting EXCEPTION ID
- );
=
= (!outInt)
=
=
=
= end;
stdIn:1576.1-1757.2 Error: syntax error: deleting RPAREN SEMICOLON
-
我嘗試添加/刪除分號在錯誤建議,但沒有奏效。
任何想法有什麼不對?
親切的問候
我會強烈建議您不要使用引用。使用它們並不會帶來什麼好處,並且您將失去進行函數式編程的全部意義! –