Where to put rescue clause

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?

James Byrne wrote:

  rescue => my_exception

Try this:

rescue Exception => e