# Anybody using begin/rescue

**URL:** <https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450>\
**Category:** Rails\
**Created:** [December 17, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450 "2007-12-17T21:41:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 17, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450/1 "2007-12-17T21:41:31Z")

</div>

i came from java world, and it is really big there.  
i don’t see a lot in ruby code samples. just a little.

anybody out there using it? or is the errors just emailed  
to you?

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 17, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450/2 "2007-12-17T21:49:13Z")

</div>

> i came from java world, and it is really big there.  
> i don’t see a lot in ruby code samples. just a little.
> 
> anybody out there using it? or is the errors just emailed  
> to you?

We use it all the time to catch things and work around it…

You probably won’t see it in samples since samples tend not to include  
much error handling…

It’s there though…

philip@MacBookPro:~/…/vendor/rails  
$ grep -r “rescue” \* | wc -l  
502

-philip

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 17, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450/3 "2007-12-17T22:54:57Z")

</div>

When I started programming ruby I hardly ever used throw/rescue  
sequences, but the more I develop and the more complex the backend  
gets the more useful I find exceptions. Many things can be handled  
with inline IF statements which i think helps to eliminate the need  
for exceptions as much, but their are still places where its cleaner/  
easier to throw an exception.

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 18, 2007, 12:01am UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450/4 "2007-12-18T00:01:10Z")

</div>

I use rescue excessively in my controller code. Even for something as  
simple  
as a show action:

def show  
@thing = Thing.find(params[:id])  
rescue ActiveRecord::RecordNotFound  
flash[:error] = “The thing you were looking for could not be found.”  
redirect\_to things\_path  
end

On Dec 18, 2007 8:24 AM, Andrew B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > i don’t see a lot in ruby code samples. just a little.
> > 
> > philip@MacBookPro:~/…/vendor/rails  
> > $ grep -r “rescue” \* | wc -l  
> > 502
> > 
> > -philip

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 18, 2007, 12:28am UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450/5 "2007-12-18T00:28:23Z")

</div>

Ryan B. wrote:

> I use rescue excessively in my controller code. Even for something as  
> simple  
> as a show action:
> 
> def show  
> @thing = Thing.find(params[:id])  
> rescue ActiveRecord::RecordNotFound  
> flash[:error] = “The thing you were looking for could not be found.”  
> redirect\_to things\_path  
> end

> –  
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)

well, dang nab. you don’t have a begin in that. is it required?  
or is it a rescue in a def, then the def is the begin of code block?

i confused.

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 18, 2007, 12:31am UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450/6 "2007-12-18T00:31:00Z")

</div>

Hi –

On Tue, 18 Dec 2007, gemblon (t.b.) wrote:

> > redirect\_to things\_path  
> > end
> 
> > –  
> > Ryan B.  
> > [http://www.frozenplague.net](http://www.frozenplague.net)
> 
> well, dang nab. you don’t have a begin in that. is it required?  
> or is it a rescue in a def, then the def is the begin of code block?

The def carries an implicit begin.

David

–  
Training for 2008!  
Ruby on Rails training by David A. Black/Ruby Power and Light, LLC:  
\* Intro to Rails, New York, NY, February 4-7 2008  
\* Advancing With Rails, New York, NY, February 11-14 2008  
Hosted by Exceed Education. See [http://www.rubypal.com](http://www.rubypal.com) for details!

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 18, 2007, 5:50am UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450/7 "2007-12-18T05:50:15Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Tue, 18 Dec 2007, gemblon (t.b.) wrote:
> 
> > > redirect\_to things\_path  
> > > end
> > 
> > > –  
> > > Ryan B.  
> > > [http://www.frozenplague.net](http://www.frozenplague.net)
> > 
> > well, dang nab. you don’t have a begin in that. is it required?  
> > or is it a rescue in a def, then the def is the begin of code block?
> 
> The def carries an implicit begin.
> 
> David
> 
> –  
> Training for 2008!  
> Ruby on Rails training by David A. Black/Ruby Power and Light, LLC:  
> \* Intro to Rails, New York, NY, February 4-7 2008  
> \* Advancing With Rails, New York, NY, February 11-14 2008  
> Hosted by Exceed Education. See [http://www.rubypal.com](http://www.rubypal.com) for details!

As in other object oriented languages, Ruby offers a mechanism for  
exception handling. I When an exception occurs. Object of class  
Exception created.

Raising Exception

begin  
#processâ€¦  
rescue  
#handle errors  
end  
We enclose the code that could raise an exception in a begin/end block  
and use rescue clauses to  
tell Ruby the types of exceptions we want to handle.

Multiple Rescue

Begin

# Process

rescue

# handle error

rescue

# handle error

end

You can have multiple rescue clauses in a begin block, and each rescue  
clause can specify multiple exceptions to catch.

hope this helps

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 18, 2007, 5:56am UTC](https://www.ruby-forum.com/t/anybody-using-begin-rescue/123450/8 "2007-12-18T05:56:13Z")

</div>

You can also use ensure to make sure certain things happen, no matter if  
an  
exception is raised or not.

On Dec 18, 2007 3:20 PM, Mohan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > Ryan B.  
> > > > –  
> > > > Raising Exception  
> > > > Multiple Rescue  
> > > > clause can specify multiple exceptions to catch.
> 
> ## hope this helps
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!
