# Where to put rescue clause

**URL:** <https://www.ruby-forum.com/t/where-to-put-rescue-clause/162427>\
**Category:** Rails\
**Created:** [March 9, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/where-to-put-rescue-clause/162427 "2009-03-09T21:46:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 9, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/where-to-put-rescue-clause/162427/1 "2009-03-09T21:46:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 10, 2009, 3:20am UTC](https://www.ruby-forum.com/t/where-to-put-rescue-clause/162427/2 "2009-03-10T03:20:07Z")

</div>

James B. wrote:

> rescue =\> my\_exception

Try this:

rescue Exception =\> e
