當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Elixir Kernel.raise用法及代碼示例

Elixir語言中 Kernel.raise 相關用法介紹如下。

用法一

raise(message)
(宏)

引發異常。

如果 message 是一個字符串,它會引發 RuntimeError 異常。

如果message 是一個原子,它隻是調用 raise/2 ,原子作為第一個參數,[] 作為第二個參數。

如果 message 是一個異常結構,則按原樣引發。

如果 message 是其他內容,則 raise 將失敗並出現 ArgumentError 異常。

例子

iex> raise "oops"
** (RuntimeError) oops

try do
  1 + :foo
rescue
  x in [ArithmeticError] ->
    IO.puts("that was expected")
    raise x
end

用法二

raise(exception, attributes)
(宏)

引發異常。

在給定參數(必須是模塊名稱,如 ArgumentError RuntimeError )上調用 exception/1 函數,傳遞 attributes 以檢索異常結構。

任何包含對 defexception/1 宏的調用的模塊都會自動實現 raise/2 預期的 Exception.exception/1 回調。有關詳細信息,請參閱 defexception/1

例子

iex> raise(ArgumentError, "Sample")
** (ArgumentError) Sample

相關用法


注:本文由純淨天空篩選整理自elixir-lang.org大神的英文原創作品 Kernel.raise(message)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。