2017-08-31 299 views
1

1> foo:inter()。 **異常錯誤:錯誤的參數 在函數foo:間/ 0(foo.erl,7號線)嘗試捕捉list_to_integer不捕獲錯誤

-module(foo). 
-compile(export_all). 

inter() -> 
    A = <<"5a">>, 
    B = binary_to_list(A), 
    try list_to_integer(B) of 
    Result -> Result 
    catch 
    _ -> {error, bad_integer} 
    end. 

我有望獲得{錯誤,bad_integer}。

回答

5

Erlang有3 types of exceptionserror,exitthrowcatch子句的格式爲Type:Pattern。當沒有指定Type時(如在您的代碼中),只有throw例外被捕獲,而list_to_integer則拋出error。您可以使用error:_捕獲所有error或使用_:_捕獲任何異常。

1> try list_to_integer("5a") of 
1> Result -> Result 
1> catch 
1> _:_ -> {error, bad_integer} 
1> end. 
{error,bad_integer}