Forum: Ruby Rescuing SyntaxError

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Caleb Tennis (Guest)
on 2005-12-17 21:04
(Received via mailing list)
Is it possible to catch illegal syntax errors?


irb(main):001:0> 550p
SyntaxError: compile error

...

# se.rb
begin
   550p
rescue SyntaxError => e
   # This never seems to get run
   puts "Caught a syntax error: " + e
end

~> ruby se.rb
blah.rb:2: syntax error

Thanks,
Caleb
Logan Capaldo (Guest)
on 2005-12-17 21:19
(Received via mailing list)
On Dec 17, 2005, at 3:04 PM, Caleb Tennis wrote:

>   550p
>
Sort of, you have to delay the compilation though:

irb(main):017:0> begin
irb(main):018:1*     eval "550p"
irb(main):019:1> rescue SyntaxError => e
irb(main):020:1>     puts "Caught a syntax error: " + e
irb(main):021:1> end
Caught a syntax error: (eval):1:in `irb_binding': compile error
(eval):1: syntax error
=> nil
Joel VanderWerf (Guest)
on 2005-12-17 22:13
(Received via mailing list)
Caleb Tennis wrote:
>   550p
> rescue SyntaxError => e
>   # This never seems to get run
>   puts "Caught a syntax error: " + e
> end
>
> ~> ruby se.rb
> blah.rb:2: syntax error
>
> Thanks,
> Caleb

[~/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.
Caleb Tennis (Guest)
on 2005-12-17 23:37
(Received via mailing list)
>
>
> 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?
Devin Mullins (Guest)
on 2005-12-18 01:11
(Received via mailing list)
Caleb Tennis wrote:

>> 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
Patrick Gundlach (Guest)
on 2005-12-18 11:48
(Received via mailing list)
> Sure thing.  Is there any way to catch it though?

Yes, when you read from a file:

begin
  require "errorfile"
rescue SyntaxError
  puts "Syntax Error!"
end


erorfile.rb:
--------------------------------------------------
puts "this is a string
--------------------------------------------------

This is what I use with my cgi scripts / PrettyException.rb

Patrick
Caleb Tennis (Guest)
on 2005-12-18 14:27
(Received via mailing list)
>
> 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.

Caleb
This topic is locked and can not be replied to.