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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 25 Aug 2008, at 08:08, Sijo Kg wrote:> > 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 asUnless 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---