From: Joel VanderWerf Date: 2005-12-18T06:12:44+09:00 Subject: Re: Rescuing SyntaxError Caleb Tennis wrote: > 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 [~/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. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407