阅读774 返回首页    go 阿里云 go 技术社区[云栖]


Ruby 1.9概要(5) 异常

1、异常的相等性,如果两个异常的class、message和backtrace一样,那么认为这两个异常是相等的,可以通过==判断。
def method
    
raise 'foobar'
end

errors 
= []
2.times do
    Thread.new do
      begin
        method
      rescue 
=> e
        errors 
<< e
      end
    end.join
end
puts errors[
-2== errors[-1]    #=> true (1.9)   false(1.8)

2、SystemStackError现在继承Exception类,而非原来的StandardError:
1.8 
 
SystemStackError < StandardError  # => true
1.9
  
SystemStackError < StandardError # => nil
   SystemStackError < Exception      #=> true

3、移除了Exception#to_str方法
begin
   
raise "foo"
rescue
   $!.to_str
end

#=> undefind method "to_str" for #<RuntimeError:foo>
文章转自庄周梦蝶  ,原文发布时间 2008-10-03

最后更新:2017-05-18 11:01:54

  上一篇:go  Ruby Tip——读文件
  下一篇:go  Ruby 1.9概要(4) Block和Proc