How to rescue an eval in Ruby?

Brent already got an answer that works, but I recommend rescuing from the smallest set of exceptions you can get away with. This makes sure you’re not accidentally gobbling up something you don’t mean to be.

Thus,

begin
  puts eval(good_str)
  puts eval(bad_str)
rescue SyntaxError => se
  puts 'RESCUED!'
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)