Skip to content

Commit

Permalink
Merge pull request #3412 from rubinius/uncaught_throw_error-3374
Browse files Browse the repository at this point in the history
Raise UncaughtThrowError for uncatched throw
  • Loading branch information
Yorick Peterse committed May 25, 2015
2 parents 7362475 + 95ab59b commit c2827aa
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
3 changes: 3 additions & 0 deletions kernel/common/exception.rb
Expand Up @@ -213,6 +213,9 @@ def to_s
end
end

class UncaughtThrowError < StandardError
end

class IndexError < StandardError
end

Expand Down
2 changes: 1 addition & 1 deletion kernel/common/throw_catch.rb
Expand Up @@ -34,7 +34,7 @@ def catch(obj = Object.new, &block)

def throw(obj, value=nil)
unless Rubinius::ThrownValue.available? obj
raise ArgumentError, "uncaught throw #{obj.inspect}"
raise UncaughtThrowError, "uncaught throw #{obj.inspect}"
end

Rubinius.throw obj, value
Expand Down
8 changes: 4 additions & 4 deletions spec/ruby/core/kernel/catch_spec.rb
Expand Up @@ -30,12 +30,12 @@
ScratchPad.recorded.should == :thrown_key
end

it "raises an ArgumentError if a Symbol is thrown for a String catch value" do
lambda { catch("exit") { throw :exit } }.should raise_error(ArgumentError)
it "raises an UncaughtThrowError if a Symbol is thrown for a String catch value" do
lambda { catch("exit") { throw :exit } }.should raise_error(UncaughtThrowError)
end

it "raises an ArgumentError if a String with different identity is thrown" do
lambda { catch("exit") { throw "exit" } }.should raise_error(ArgumentError)
it "raises an UncaughtThrowError if a String with different identity is thrown" do
lambda { catch("exit") { throw "exit" } }.should raise_error(UncaughtThrowError)
end

it "catches a Symbol when thrown a matching Symbol" do
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/core/kernel/throw_spec.rb
Expand Up @@ -41,8 +41,8 @@
res.should == :return_value
end

it "raises an ArgumentError if there is no catch block for the symbol" do
lambda { throw :blah }.should raise_error(ArgumentError)
it "raises an UncaughtThrowError if there is no catch block for the symbol" do
lambda { throw :blah }.should raise_error(UncaughtThrowError)
end

it "raises ArgumentError if 3 or more arguments provided" do
Expand Down
12 changes: 6 additions & 6 deletions spec/ruby/language/throw_spec.rb
Expand Up @@ -44,7 +44,7 @@
end

it "does not convert strings to a symbol" do
lambda { catch(:exit) { throw "exit" } }.should raise_error(ArgumentError)
lambda { catch(:exit) { throw "exit" } }.should raise_error(UncaughtThrowError)
end

it "unwinds stack from within a method" do
Expand All @@ -62,18 +62,18 @@ def throw_method(handler,val)
catch(:foo) { c.call }.should == :msg
end

it "raises an ArgumentError if outside of scope of a matching catch" do
lambda { throw :test,5 }.should raise_error(ArgumentError)
lambda { catch(:different) { throw :test,5 } }.should raise_error(ArgumentError)
it "raises an UncaughtThrowError if outside of scope of a matching catch" do
lambda { throw :test,5 }.should raise_error(UncaughtThrowError)
lambda { catch(:different) { throw :test,5 } }.should raise_error(UncaughtThrowError)
end

it "raises an ArgumentError if used to exit a thread" do
it "raises an UncaughtThrowError if used to exit a thread" do
lambda {
catch(:what) do
Thread.new do
throw :what
end.join
end
}.should raise_error(ArgumentError)
}.should raise_error(UncaughtThrowError)
end
end
16 changes: 8 additions & 8 deletions spec/ruby/optional/capi/kernel_spec.rb
Expand Up @@ -87,8 +87,8 @@
ScratchPad.recorded.should == [:before_throw]
end

it "raises an ArgumentError if there is no catch block for the symbol" do
lambda { @s.rb_throw(nil) }.should raise_error(ArgumentError)
it "raises an UncaughtThrowError if there is no catch block for the symbol" do
lambda { @s.rb_throw(nil) }.should raise_error(UncaughtThrowError)
end
end

Expand All @@ -113,8 +113,8 @@
ScratchPad.recorded.should == [:before_throw]
end

it "raises an ArgumentError if there is no catch block for the symbol" do
lambda { @s.rb_throw(nil) }.should raise_error(ArgumentError)
it "raises an UncaughtThrowError if there is no catch block for the symbol" do
lambda { @s.rb_throw(nil) }.should raise_error(UncaughtThrowError)
end
end

Expand Down Expand Up @@ -296,8 +296,8 @@
ScratchPad.recorded.should == [:before_throw]
end

it "raises an ArgumentError if the throw symbol isn't caught" do
lambda { @s.rb_catch("foo", lambda { throw :bar }) }.should raise_error(ArgumentError)
it "raises an UncaughtThrowError if the throw symbol isn't caught" do
lambda { @s.rb_catch("foo", lambda { throw :bar }) }.should raise_error(UncaughtThrowError)
end
end

Expand All @@ -322,8 +322,8 @@
ScratchPad.recorded.should == [:before_throw]
end

it "raises an ArgumentError if the throw symbol isn't caught" do
lambda { @s.rb_catch("foo", lambda { throw :bar }) }.should raise_error(ArgumentError)
it "raises an UncaughtThrowError if the throw symbol isn't caught" do
lambda { @s.rb_catch("foo", lambda { throw :bar }) }.should raise_error(UncaughtThrowError)
end
end

Expand Down

0 comments on commit c2827aa

Please sign in to comment.