[~/tmp] cat se2.rb
begin
eval <<-END
begin
550p
rescue SyntaxError => e
# This never seems to get run
puts "Caught a syntax error: " + e
end
END
rescue SyntaxError
puts “Caught a syntax error during eval”
end
[~/tmp] ruby se2.rb
Caught a syntax error during eval
The reason the inner rescue is not triggered is that the exception
occurs while eval is turning the string into a parse tree, before it
starts executing the parse tree.
The reason the inner rescue is not triggered is that the exception
occurs while eval is turning the string into a parse tree, before it
starts executing the parse tree.
No. How is Ruby supposed to know that the rescue block isn’t
supposed to be part of that unterminated puts string from the line
before? Do the eval, load, or require thing as suggested.
Yeah, I will. That’s what will work in what I’m trying to do
anyway. Thanks.
The reason the inner rescue is not triggered is that the exception
occurs while eval is turning the string into a parse tree, before it
starts executing the parse tree.
Sure thing. Is there any way to catch it though?
begin
puts "this is text
rescue SyntaxError
puts “Syntax Error!”
end END
No. How is Ruby supposed to know that the rescue block isn’t supposed to
be part of that unterminated puts string from the line before? Do the
eval, load, or require thing as suggested.
Devin
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.