# To catch exception from update\_attribute

**URL:** <https://www.ruby-forum.com/t/to-catch-exception-from-update-attribute/146766>\
**Category:** Rails\
**Created:** [August 25, 2008, 9:08am UTC](https://www.ruby-forum.com/t/to-catch-exception-from-update-attribute/146766 "2008-08-25T09:08:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 25, 2008, 9:08am UTC](https://www.ruby-forum.com/t/to-catch-exception-from-update-attribute/146766/1 "2008-08-25T09:08:57Z")

</div>

Hi  
begin  
ActiveRecord::Base.transaction do  
pb\_status\_history = ProblemStatusHistory.new  
pb\_status\_history.problem\_id = @problem.id  
pb\_status\_history.problem\_status\_id =  
@problem.problem\_status\_id  
pb\_status\_history.save!  
ret\_val=@problem.update\_attribute(“problem\_status\_id”,‘ab’)  
if ret\_val==false  
raise ActiveRecord::ActiveRecordError  
end  
end # end of transaction  
rescue  
puts 'error is '+$!  
end  
What I was trying to do above was to write a proper exception  
handling for the above code.In Database table problem\_status\_id is an  
integer field And I deliberatly wrote  
@problem.update\_attribute(“problem\_status\_id”,‘ab’) So what i expect was  
an exception there and which will be caught by the raised as  
raise ActiveRecord::ActiveRecordError  
and will be caught by rescue…But that did not happen.Could u please  
tell me where the error is .Also in above I am using save! and put the  
whole code in transaction block…So if suppose pb\_status\_history.save!  
also results an exception need I to write another rescue for that also  
or a single rescue do all these?

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 25, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/to-catch-exception-from-update-attribute/146766/2 "2008-08-25T14:34:29Z")

</div>

On 25 Aug 2008, at 08:08, Sijo Kg wrote:

> ```
> if ret_val==false
> 
> ```
> 
> was  
> an exception there and which will be caught by the raised as  
> Unless you’ve got a validation or a foreign key constraint  
> ActiveRecord won’t care about that (i expect it would typecast it to 0  
> or something like that)

> raise ActiveRecord::ActiveRecordError  
> and will be caught by rescue…But that did not happen.Could u please  
> tell me where the error is .Also in above I am using save! and put  
> the  
> whole code in transaction block…So if suppose pb\_status\_history.save!  
> also results an exception need I to write another rescue for that  
> also  
> or a single rescue do all these?

You don’t need more than one rescue block although if you wanted to do  
different things in response to different exceptions then you’d  
typically have multiple rescue blocks. Another thing to note (although  
not relevant here) is that rescue with no arguments does not actually  
rescue all exceptions, only those that descend from StandardError.

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 25, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/to-catch-exception-from-update-attribute/146766/3 "2008-08-25T20:38:50Z")

</div>

You may want to try explicitly putting the word Exception in the rescue  
statement as follows:

begin  
.  
.  
rescue Exception  
… your exception handing code, at least print out the sub-class  
end

Once you determine which particular exception is actually being raised,  
you may want to further refine your rescue clause and catch it before  
you fall through to the generic Exception.

I do know that you don’t need to explicitly mention a target for the  
“rescue” statement. However, I have found it to be more consistent to  
use at least a catch all Exception word explicitly.  
Hope this helps.  
Bharat
