⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 01 - raising an exception.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -