# Rails exception catching

**URL:** <https://www.ruby-forum.com/t/rails-exception-catching/111823>\
**Category:** Rails\
**Created:** [August 11, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/rails-exception-catching/111823 "2007-08-11T17:50:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 11, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/rails-exception-catching/111823/1 "2007-08-11T17:50:52Z")

</div>

Hello I’m writting my first rails application and I would like to catch  
when an id cant be found.  
For instance have create records 1 2 and 3

I would like to:  
records=Record.find(4)  
and not get the error page rather somthing like

records=Record.find(4)

if (records==nil)  
…  
do stuff  
…  
else  
…  
do other stuf  
…  
end

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 11, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/rails-exception-catching/111823/2 "2007-08-11T18:02:07Z")

</div>

On 8/11/07, removed\_email\_address@domain.invalid Mr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> if (records==nil)  
> …  
> do stuff  
> …  
> else  
> …  
> do other stuf  
> …  
> end

You use rescue and read this chapter about Ruby Exceptions [1].

Good luck!

[1] [http://www.rubycentral.com/pickaxe/tut\_exceptions.html](http://www.rubycentral.com/pickaxe/tut_exceptions.html)

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 11, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/rails-exception-catching/111823/3 "2007-08-11T19:15:23Z")

</div>

Mike De wrote:

> Hello I’m writting my first rails application and I would like to catch  
> when an id cant be found.  
> For instance have create records 1 2 and 3
> 
> I would like to:  
> records=Record.find(4)  
> and not get the error page rather somthing like
> 
> records=Record.find(4)
> 
> if (records==nil)  
> …  
> do stuff  
> …  
> else  
> …  
> do other stuf  
> …  
> end

You probably want something like:

begin  
record = Record.find(14)  
rescue ActiveRecord::RecordNotFound  
puts ‘Record 14 not Found!’  
end

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 13, 2007, 3:41pm UTC](https://www.ruby-forum.com/t/rails-exception-catching/111823/4 "2007-08-13T15:41:28Z")

</div>

Thank you both perfect.

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 13, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/rails-exception-catching/111823/5 "2007-08-13T23:20:24Z")

</div>

Mike De wrote:

> Hello I’m writting my first rails application and I would like to catch  
> when an id cant be found.  
> For instance have create records 1 2 and 3
> 
> I would like to:  
> records=Record.find(4)  
> and not get the error page rather somthing like
> 
> records=Record.find(4)
> 
> if (records==nil)  
> …  
> do stuff  
> …  
> else  
> …  
> do other stuf  
> …  
> end

Here’s the thing: the reason that AR is throwing an exception when that  
fails is because you’re specifically giving it an ID. The assumption is  
that you have an ID, you know the record exists, and therefore if it  
can’t be found, it’s an exceptional situation.

If the records attached to those IDs are going away for some reason,  
either you are stuck using a rescue, or perhaps you can refactor your  
problem so that you are making sure whatever dangling objects still have  
those ids are cleaned up before you actually try to find them.
