Forum: Ruby on Rails Where to put rescue clause

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.
James B. (Guest)
on 2009-03-09 22:46
I have a unique index constraint that throws an
ActiveRecord::StatementInvalid exception when triggered.  In my
application this causes a stack trace in the client browser.  I want to
rescue this error and handle it inside the controller, but I cannot seem

Here is the controller code:

  def create
    @entity =[:entity])
    respond_to do |format|
        flash[:notice] = 'Client was successfully created.'
        format.html { redirect_to(@client) }
        format.xml  { render :xml => @client,
          :status => :created, :location => @client }
        format.html { render :action => "new" }
        format.xml  { render :xml => @client.errors,
          :status => :unprocessable_entity }
  rescue => my_exception
    puts "Rescue clause invoked!"
    puts my_exception

For now, all I want is to see the rescue clause invoked, but it is not.
have I put this clause in the wrong place? Am I specifying it wrong?
Jeremy W. (Guest)
on 2009-03-10 04:20
James B. wrote:

>   rescue => my_exception

Try this:

rescue Exception => e
This topic is locked and can not be replied to.