当前位置: 动力学知识库 > 问答 > 编程问答 >

exception handling - Rails 3 - Rescuing a Mysql2::Error: Table doesn't exist

问题描述:

I have an iteration in which I am calling set_table_name for some model. The idea is that in each iteration, the model changes its table. Sometimes, the table won't exist, in that case, an error of this kind will happen:

Mysql2::Error: Table 'db_name.table_name_xyz' doesn't exist

I want the iteration to keep running and not abort because of the error. I've wrapped the set_table_name line of code with a begin and rescue, but no exception seems to be raised as the script instantly aborts on error (it doesn't execute the rescue code). Here's the code:

((start_year)..(start_actual_year)).each do |year|

begin

Data.set_table_name("Secciones#{year}#{year + 1}")

rescue Exception => e

next

end

end

Can I rescue this kind of error? What should I do? Thanks!

网友答案:

Are you sure you are catching the exception correctly? Here's more info if needed.

begin
  # Your code here.
rescue Exception => e
  # Output the exception that it is catching.
  puts e.message
ensure
  # The ensure block is used to close a db connection if needed.
end
分享给朋友:
您可能感兴趣的文章:
随机阅读: