# Using exceptions on all of my controller actions

**URL:** <https://www.ruby-forum.com/t/using-exceptions-on-all-of-my-controller-actions/114294>\
**Category:** Rails\
**Created:** [September 11, 2007, 12:12am UTC](https://www.ruby-forum.com/t/using-exceptions-on-all-of-my-controller-actions/114294 "2007-09-11T00:12:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jojojobbins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jojojobbins](https://www.ruby-forum.com/u/jojojobbins)\
**Post date:** [September 11, 2007, 12:12am UTC](https://www.ruby-forum.com/t/using-exceptions-on-all-of-my-controller-actions/114294/1 "2007-09-11T00:12:07Z")

</div>

Hello guys, I was wondering how good is it to use exceptions for  
everything in your rails application.

I mean like using save! everytime instead of the plain save. Or using  
begin and rescue in every action in your controller.

Is this adviseable? does it hit the application on the performance side?  
Is it better to avoid them and use them just when really necessary?

I will appreciate any comments you may have on this. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jojojobbins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jojojobbins](https://www.ruby-forum.com/u/jojojobbins)\
**Post date:** [September 11, 2007, 7:32am UTC](https://www.ruby-forum.com/t/using-exceptions-on-all-of-my-controller-actions/114294/2 "2007-09-11T07:32:43Z")

</div>

Ok, here is a little example to make things clearer:

This is the ordinary form:  
def update  
@user = User.find( params[:id] )  
if @user.update\_attributes( params[:user] )  
flash[:notice] = ‘User was successfully updated.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘edit’  
end  
end

This is the form I prefer, but I don’t know if I’m paying an excesive  
price using exceptions instead of simple if’s.  
def update  
begin  
@user = User.find( params[:id] )  
@old\_name = @user.login  
@user.update\_attributes!( params[:user] )  
flash[:notice] = update\_ok( @user.login )  
redirect\_to :action =\> ‘list’  
rescue ActiveRecord::RecordNotFound =\> e  
flash[:error] = find\_error  
redirect\_to :action =\> ‘list’  
rescue =\> e  
flash[:error] = update\_error( @old\_name )  
render :action =\> ‘edit’  
end  
end

What do you think?
