# Exception handling

**URL:** <https://www.ruby-forum.com/t/exception-handling/187584>\
**Category:** Rails\
**Created:** [April 26, 2010, 10:22am UTC](https://www.ruby-forum.com/t/exception-handling/187584 "2010-04-26T10:22:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 26, 2010, 10:22am UTC](https://www.ruby-forum.com/t/exception-handling/187584/1 "2010-04-26T10:22:24Z")

</div>

Hi  
I have controller edit action like

def edit  
begin  
@user = User.find(params[:id])  
rescue ActiveRecord::RecordNotFound  
logger.warn “User with id #{current\_user.id} tried to edit user with  
id #{params[:id]}”  
render :file =\> “#{RAILS\_ROOT}/public/404.html”  
else  
-------  
end  
end

```
And suppose if current_user has noa ccess to user with id = 10.Then

```

when try  
[http://localhost:3000/edit/10](http://localhost:3000/edit/10)  
This will render the 404.html

```
 Now my problem is suppose if the user try something like

```

[http://localhost:3000/edi123t/10](http://localhost:3000/edi123t/10)  
Then what I get is a routing error. I dont know how to handle this.  
Please help.

Thanks  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 26, 2010, 10:52am UTC](https://www.ruby-forum.com/t/exception-handling/187584/2 "2010-04-26T10:52:40Z")

</div>

Hi  
Thanks. But how can I handle in code?

Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 26, 2010, 10:28am UTC](https://www.ruby-forum.com/t/exception-handling/187584/3 "2010-04-26T10:28:06Z")

</div>

Well you can specify a default page / controller action to perform for  
unmatched routes as well…so if the route doesn’t exist it shows a much  
user friendly page instead of the rails error message

Thanks & Regards,  
Dhruva S…

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 27, 2010, 9:46am UTC](https://www.ruby-forum.com/t/exception-handling/187584/4 "2010-04-27T09:46:04Z")

</div>

Hi,

In application controller

rescue\_from  
ActiveRecord::RecordNotFound,ActionController::RoutingError,  
ActionController::UnknownController,  
ActionController::UnknownAction, ::NoMethodError,  
:with =\> :handle\_exception

# To handle Exceptions

def handle\_exception  
redirect\_to no\_access\_url # redirect to a page with u need show  
end
