# begin/rescue not working????

**URL:** <https://discuss.rubyonrails.org/t/begin-rescue-not-working/19895>\
**Category:** rubyonrails-talk\
**Created:** [October 31, 2007, 1:56pm UTC](https://discuss.rubyonrails.org/t/begin-rescue-not-working/19895 "2007-10-31T13:56:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_Showalter](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@Bob\_Showalter](https://discuss.rubyonrails.org/u/Bob_Showalter)\
**Post date:** [October 31, 2007, 1:56pm UTC](https://discuss.rubyonrails.org/t/begin-rescue-not-working/19895/1 "2007-10-31T13:56:23Z")

</div>

> Can someone help me with the following code? I'm purposely typing in a bogus rga number, and the begin/rescue block should catch that and inform the user, right?

It does; that's not the problem.

> But when I run this with a bogus number, I get the "Render and/or redirect were called multiple times in this action" error.
> 
> What am I doing wrong here?
> 
> Thanks!
> 
> def assign &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find(:all, :order =\> 'name')
> 
> &nbsp;&nbsp;if request.post? &nbsp;&nbsp;&nbsp;&nbsp;rga\_number = params[:rga\_number] &nbsp;&nbsp;&nbsp;&nbsp;user\_id = params[:user\_id]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;begin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rga = Rga.find(params[:rga\_number]) &nbsp;&nbsp;&nbsp;&nbsp;rescue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'There is not a RGA with that number' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'list'

You need to "return" right here, or execution continues on. That's why you're getting the error message.

You probably should rescue ActiveRecord::RecordNotFound. If a different exception were to be raised, you would never know it.

---

<div class="post-metadata">

**Author:** ![Bob\_Showalter](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@Bob\_Showalter](https://discuss.rubyonrails.org/u/Bob_Showalter)\
**Post date:** [October 31, 2007, 2:04pm UTC](https://discuss.rubyonrails.org/t/begin-rescue-not-working/19895/2 "2007-10-31T14:04:11Z")

</div>

I mean to add a "return" statement:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'list' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return # \<-- add this line &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [October 31, 2007, 2:21pm UTC](https://discuss.rubyonrails.org/t/begin-rescue-not-working/19895/3 "2007-10-31T14:21:18Z")

</div>

This has nothing to do with begin/rescue. It has everything to do with the fact that you should redirect or render precisely once per action, whereas in you original code you would end up doing a redirect in the rescue block and then again if bit below.

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [October 31, 2007, 2:50pm UTC](https://discuss.rubyonrails.org/t/begin-rescue-not-working/19895/4 "2007-10-31T14:50:28Z")

</div>

OK, so at a basic level, this will result in the double render error

def foo &nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'bar' end

so def foo &nbsp;&nbsp;&nbsp;if ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'bar' &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;if ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'bar' &nbsp;&nbsp;&nbsp;end end

will also cause errors unless the 2 conditions are mutually exclusive

You need to do something like. Either return after redirecting/rendering or do whatever you have to do to ensure that you don't do it more than once per action, for example

def foo &nbsp;&nbsp;&nbsp;if ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'bar' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;if ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'bar' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &nbsp;&nbsp;&nbsp;end end

Fred
