Sha256: c9ca56965658eece8f63f5315b71c1e4ae8678aac2248eb4c249849f9f83b385

Contents?: true

Size: 1.92 KB

Versions: 83

Compression:

Stored size: 1.92 KB

Contents

require File.dirname(File.join(__rhoGetCurrentDir(), __FILE__)) + '/../spec_helper'
class SpecificExampleException < StandardError
end
class OtherCustomException < StandardError
end

def exception_list
  [SpecificExampleException, ZeroDivisionError]
end
describe "The rescue keyword" do
  it "can be used to handle a specific exception" do
    lambda do
      begin
        raise SpecificExampleException, "Raising this to be handled below"
      rescue SpecificExampleException
      end
    end.should_not raise_error
  end
  
  it "can capture the raised exception in a local variable" do
    begin
      raise SpecificExampleException, "some text"
    rescue SpecificExampleException => e
      e.message.should == "some text"
    end
  end
  
  it "can rescue multiple raised exceptions with a single rescue block" do
    lambda do
      [lambda{1/0}, lambda{raise SpecificExampleException}].each do |block|
        begin
          block.call
        rescue SpecificExampleException, ZeroDivisionError
        end
      end
    end.should_not raise_error
  end
  
  it "can rescue a splatted list of exceptions" do
    caught_it = false
    begin
      raise SpecificExampleException, "not important"
    rescue *exception_list
      caught_it = true
    end
    caught_it.should be_true
    caught = []
    lambda do
      [lambda{1/0}, lambda{raise SpecificExampleException}].each do |block|
        begin
          block.call
        rescue *exception_list
          caught << $!
        end
      end
    end.should_not raise_error    
    caught.size.should == 2
    exception_list.each do |exception_class|
      caught.map{|e| e.class}.include?(exception_class).should be_true
    end
  end
  
  it "will only rescue the specified exceptions when doing a splat rescue" do
    lambda do
      begin
        raise OtherCustomException, "not rescued!"
      rescue *exception_list
      end
    end.should raise_error(OtherCustomException)
  end
  
end

Version data entries

83 entries across 83 versions & 1 rubygems

Version Path
rhodes-3.1.1 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.1.1.beta spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.1.0 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.1.0.beta.5 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.1.0.beta.4 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.1.0.beta.3 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.1.0.beta.2 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.1.0.beta.1 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.2 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.2.beta.1 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.1 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.1.beta.8 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.1.beta.7 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.1.beta.6 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.1.beta.5 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.1.beta.4 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.1.beta.3 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.1.beta.2 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.0 spec/framework_spec/app/spec/language/rescue_spec.rb
rhodes-3.0.0.beta.7 spec/framework_spec/app/spec/language/rescue_spec.rb