# Why does unsuccesful find throw an exception?

**URL:** <https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079>\
**Category:** Rails\
**Created:** [September 8, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079 "2006-09-08T23:42:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [September 8, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079/1 "2006-09-08T23:42:09Z")

</div>

When querying my DB via find such as in:

@user = User.find(:first, :conditions =\> etc)

why do I get an exception when the find is unsuccessful? I thought the  
regular behavior would be to just set @user to nil. can I change the way  
this operates?

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [September 9, 2006, 1:43am UTC](https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079/2 "2006-09-09T01:43:22Z")

</div>

On 9/8/06, Mike K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When querying my DB via find such as in:
> 
> @user = User.find(:first, :conditions =\> etc)
> 
> why do I get an exception when the find is unsuccessful? I thought the  
> regular behavior would be to just set @user to nil. can I change the way  
> this operates?

Wow, you shouldn’t be getting an exception, at least not with :first.  
The agile book specifically points this out! (First edition, anyway;  
page 219 sidebar for those following at home.)

Find’ing with an id SHOULD throw an exception though; are you  
specifying id in your conditions?

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [September 9, 2006, 1:46am UTC](https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079/3 "2006-09-09T01:46:54Z")

</div>

Actually it was not a :first it was a find by ID with some conditions  
and the conditions weren’t met although the id was present.

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [September 9, 2006, 1:47am UTC](https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079/4 "2006-09-09T01:47:49Z")

</div>

But I’m still not clear. Should I have an exception handler for this  
condition to take control? Or should I never search unless I know it  
should be succesful?

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [September 9, 2006, 2:21am UTC](https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079/5 "2006-09-09T02:21:26Z")

</div>

On 9/8/06, Mike K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But I’m still not clear. Should I have an exception handler for this  
> condition to take control? Or should I never search unless I know it  
> should be succesful?

Well if your code is throwing an exception then yeah, you should  
handle exceptions 😉

How about a simple:

begin  
@user = User.find(:first)  
rescue  
flash[:notice] = ‘Sorry, fresh out of users!’  
redirect\_to index\_url  
end

That is, of course, assuming the User.find(:first) call would throw an  
exception if there are no users. I know it does for certain if you  
specify in an invalid user though…

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [September 9, 2006, 5:02pm UTC](https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079/6 "2006-09-09T17:02:50Z")

</div>

Thank you sir. I have gone and gotten a lot smarter about Ruby and  
exceptions. I’m very familiar with this as an error-handling mechanism,  
I just did not realize Ruby in general had such a robust architecture in  
this regard. Since my first encounter with Ruby has been with Rails I am  
finding more things to be impressed about the Ruby architecture the  
further I go. I should also read the Find documentation a bit closer 😉

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [September 9, 2006, 2:23am UTC](https://www.ruby-forum.com/t/why-does-unsuccesful-find-throw-an-exception/68079/7 "2006-09-09T02:23:26Z")

</div>

On 9/8/06, Mike K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But I’m still not clear. Should I have an exception handler for this  
> condition to take control? Or should I never search unless I know it  
> should be succesful?

If you’re finding by id, then yes, you should be handling exceptions.  
If you’re not finding by id, then if nothing’s there, you should  
handle that eventuality accordingly.

So say the docs:  
([ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000860))

```
* Find by id: This can either be a specific id (1), a list of ids

```

(1, 5, 6), or an array of ids ([5, 6, 10]). If no record can be found  
for all of the listed ids, then RecordNotFound will be raised.  
\* Find first: This will return the first record matched by the  
options used. These options can either be specific conditions or  
merely an order. If no record can matched, nil is returned.  
\* Find all: This will return all the records matched by the  
options used. If no records are found, an empty array is returned.
