2012-04-24 30 views
5

以下可能嗎?在OCaml中處理多種異常類型

try 
    (* danger zone *) 
with Not_found e -> 
    (* code to handle not found *) 
with t -> 
    (* code to handle all other issues *) 

如果我在頂級類型中輸入該值,則會在第二個with上出現語法錯誤。也許有一些我不知道的語法?

爲了匹配每個with,是否優先選擇另一個try

+0

是什麼使你認爲你寫的語法應該工作?你在別人的代碼中看到過嗎?或者手冊或教程中有這樣的例子嗎?猜測不是一個很好的學習方式。 – ygrek 2012-04-25 07:50:57

回答

5

withmatch表達式;您不重複多個模式,而是使用|來分隔每個模式->表達式,與match一樣。

13

with部分是一系列的圖案,讓你可以這樣寫如下:

try 
    (* dangerous code area *) 
with 
    | Not_found -> (* Not found handling code *) 
    | t -> (* Handle other issues here *)