本文简要介绍ruby语言中 Kernel.fail 的用法。
用法
fail
fail(string, cause: $!)
fail(exception [, string [, array]], cause: $!)
别名:raise
如果没有参数,则在 $! 中引发异常,或者如果 $! 为 nil 则引发 RuntimeError 。使用单个 String 参数,引发带有字符串作为消息的 RuntimeError。否则,第一个参数应该是一个Exception 类(或另一个在发送exception 消息时返回Exception 对象的对象)。可选的第二个参数设置与异常关联的消息(可通过 Exception#message 访问),第三个参数是回调信息数组(可通过 Exception#backtrace 访问)。生成的异常(可通过 Exception#cause 访问)的 cause 会自动设置为 “current” 异常( $! ),如果有的话。可以通过 :cause 参数指定替代值,即 Exception 对象或 nil 。
begin...end 块的rescue 子句捕获异常。
raise "Failed to create socket"
raise ArgumentError, "No parameters", caller
相关用法
- Ruby Kernel.frozen?用法及代码示例
- Ruby Kernel.format用法及代码示例
- Ruby Kernel.local_variables用法及代码示例
- Ruby Kernel.Integer用法及代码示例
- Ruby Kernel.binding用法及代码示例
- Ruby Kernel.`cmd`用法及代码示例
- Ruby Kernel.autoload用法及代码示例
- Ruby Kernel.loop用法及代码示例
- Ruby Kernel.Hash用法及代码示例
- Ruby Kernel.caller用法及代码示例
- Ruby Kernel.set_trace_func用法及代码示例
- Ruby Kernel.exit!用法及代码示例
- Ruby Kernel.trap用法及代码示例
- Ruby Kernel.String用法及代码示例
- Ruby Kernel.select用法及代码示例
- Ruby Kernel.syscall用法及代码示例
- Ruby Kernel.then用法及代码示例
- Ruby Kernel.sprintf用法及代码示例
- Ruby Kernel.Pathname用法及代码示例
- Ruby Kernel.srand用法及代码示例
- Ruby Kernel.yield_self用法及代码示例
- Ruby Kernel.BigDecimal用法及代码示例
- Ruby Kernel.raise用法及代码示例
- Ruby Kernel.test用法及代码示例
- Ruby Kernel.pretty_inspect用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Kernel.fail。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
