begin
Model.transaction do
model.save!
# 手工抛异常
raise ActiveRecord::ActiveRecordError if ...
end
rescue ActiveRecord::ActiveRecordError
# 返回错误信息...
end
注意,捕获的异常是ActiveRecord::ActiveRecordError
,而不是StandardError
或者RuntimeError