# Ruby exception handling

**URL:** <https://www.ruby-forum.com/t/ruby-exception-handling/146130>\
**Category:** Ruby\
**Created:** [August 18, 2008, 10:37am UTC](https://www.ruby-forum.com/t/ruby-exception-handling/146130 "2008-08-18T10:37:23Z")\
**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 18, 2008, 10:37am UTC](https://www.ruby-forum.com/t/ruby-exception-handling/146130/1 "2008-08-18T10:37:23Z")

</div>

Hi  
I have been learning exception handling in ruby.I am not familiar  
with the exception handling mechanisms (The project which I am working  
is my first project and it is in Ruby)…Could anyone explain me how to  
add exception handling coding for the following fragment Then it will be  
very valuable for me to understand exception handling mechanisms in  
ruby.Here what I am doing is first add an activity to the  
problem\_activities table (which has problem\_id as one of its field) and  
if there is attachment for that activity, that is added to  
problem\_activity\_attachments table (which has problem\_activity\_id as one  
of its column)

def pb\_activity\_attach\_file  
@problem=Problem.find(params[:id])  
ActiveRecord::Base.transaction do  
@pb\_activity=ProblemActivity.new(params[:pb\_activity])  
@pb\_activity.problem\_id= params[:id]  
save\_result=@pb\_activity.save!  
if !params[:pb\_activity\_attachment][:attachment].blank?  
if save\_result  
@pb\_activity\_attachment=ProblemActivityAttachment.new(params[:pb\_activity\_attachment])  
@pb\_activity\_attachment.problem\_activity\_id=@pb\_activity.id.to\_i  
@pb\_activity\_attachment.save!  
end  
end  
end #end of transaction  
@pb\_activities=@problem.problem\_activities.pb\_activities\_paginate(params[:page])  
responds\_to\_parent do  
render :update do |page|  
page[:pb\_create\_activity\_ui].replace :partial =\>  
“problem\_part/view\_pb\_activity\_details”, :object =\> @pb\_activities,  
:object =\> @problem  
end  
end #respond\_to do end  
end

---

<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 18, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/ruby-exception-handling/146130/2 "2008-08-18T22:23:06Z")

</div>

Sijo Kg wrote:  
here ya go:

> def pb\_activity\_attach\_file  
> begin  
> @problem=Problem.find(params[:id])  
> ActiveRecord::Base.transaction do  
> @pb\_activity=ProblemActivity.new(params[:pb\_activity])  
> @pb\_activity.problem\_id= params[:id]  
> save\_result=@pb\_activity.save!  
> if !params[:pb\_activity\_attachment][:attachment].blank?  
> if save\_result  
> @pb\_activity\_attachment=ProblemActivityAttachment.new(params[:pb\_activity\_attachment])  
> @pb\_activity\_attachment.problem\_activity\_id=@pb\_activity.id.to\_i  
> @pb\_activity\_attachment.save!  
> end  
> end  
> end #end of transaction  
> @pb\_activities=@problem.problem\_activities.pb\_activities\_paginate(params[:page])  
> responds\_to\_parent do  
> render :update do |page|  
> page[:pb\_create\_activity\_ui].replace :partial =\>  
> “problem\_part/view\_pb\_activity\_details”, :object =\> @pb\_activities,  
> :object =\> @problem  
> end  
> end #respond\_to do end

```
 rescue Exception => e
    # can retry, or raise or what not
 end

```

> end

---

<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 19, 2008, 6:23am UTC](https://www.ruby-forum.com/t/ruby-exception-handling/146130/3 "2008-08-19T06:23:28Z")

</div>

Hi  
Thanks for your reply.Actually I expect some elaborate reply.So I  
need to put the whole def in one begin-end…Why i expect such some  
elaborate reply means actually i am new to exception handling in any  
language(Since as I told this is my first project ) Again thanks for the  
reply

Sijo
