01 - raising an exception.rb
来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 49 行
RB
49 行
def raise_exception puts 'I am before the raise.' raise 'An error has occurred.' puts 'I am after the raise.'endraise_exception# I am before the raise.# RuntimeError: An error has occurred#---def inverse(x) raise "Argument is not numeric" unless x.is_a? Numeric 1.0 / xend#---inverse(2) # => 0.5#---inverse('not a number')# RuntimeError: Argument is not numeric#---ObjectSpace.each_object(Class) do |x| puts x if x.ancestors.member? Exceptionend#---ObjectSpace.each_object(Class) { |x| puts x if x.name =~ /Error$/ }# SystemStackError# LocalJumpError# EOFError# IOError# RegexpError# ...#---def inverse(x) raise ArgumentError, 'Argument is not numeric' unless x.is_a? Numeric 1.0 / xend#---class NotInvertibleError < StandardErrorend#---def inverse(x) raise NotInvertibleError, 'Argument is not numeric' unless x.is_a? Numeric 1.0 / xendinverse('not a number')# NotInvertibleError: Argument is not numeric#---
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?