Forum: Ruby on Rails handling specific database exceptions

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Alon (Guest)
on 2006-05-30 17:45

When an exception comes from rails, it has a name, i.e:
"AssociationTypeMismatch", so I can catch it like this:

    rescue ActiveRecord::AssociationTypeMismatch => e

However, how can I catch and handle specific exceptions that come from
the database. "rescue Exception => e" is not good enough. I want to be
able to deal with them individually. for example, mysql throws a
duplicate entry key error, how do I catch that in rails?

Alon (Guest)
on 2006-05-30 21:57
I am trying to figure out if I need to use the database specific error
codes, but I can't figure out how to access it from rails...
This topic is locked and can not be replied to.