Forum: Ruby on Rails Where to put rescue clause

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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
to.

Here is the controller code:

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


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.