# Handling/Catching Exceptions

**URL:** <https://www.ruby-forum.com/t/handling-catching-exceptions/55095>\
**Category:** Rails\
**Created:** [March 16, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/handling-catching-exceptions/55095 "2006-03-16T23:26:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [March 16, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/handling-catching-exceptions/55095/1 "2006-03-16T23:26:09Z")

</div>

Hello!

I was wondering, sometimes I raise an exception in my Rails apps, e.g.:

raise SecurityError unless @post.user == logged\_in\_user

Now: is there any way to catch that SecurityError exception somewhere  
and render a custom template? E.g., I want to render  
‘app/views/content/security\_error.rhtml’

Does anyone know how to do this?

Thanks a bunch,  
Rob

---

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [March 16, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/handling-catching-exceptions/55095/2 "2006-03-16T23:41:33Z")

</div>

Hi Rob ~

You could put it in a begin/rescue block…

begin  
SOMETHING THAT MIGHT FAIL HERE  
rescue  
WHEN IT FAILS DO STUFF HERE  
end

~ Ben

On 3/16/06, Robert MannI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Ben R.  
[http://www.benr75.com](http://www.benr75.com)

---

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [March 17, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/handling-catching-exceptions/55095/3 "2006-03-17T14:41:40Z")

</div>

i would be interested in this as well. let me know if you find  
anything.

adam

---

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [March 17, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/handling-catching-exceptions/55095/4 "2006-03-17T14:38:17Z")

</div>

Hi Ben!

Thanks, yeah. But I was wondering if there’s a way to globally catch  
them. Doesn’t Rails catch things like ActiveRecord::RecordNotFound?  
Maybe one can plugin custom error catching there.

If I find anything I will update this thread.

Thanks,  
Rob

---

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [March 18, 2006, 10:34am UTC](https://www.ruby-forum.com/t/handling-catching-exceptions/55095/5 "2006-03-18T10:34:38Z")

</div>

On 17 Mar 2006, at 14:39, Adam D. wrote:

> i would be interested in this as well. let me know if you find  
> anything.

It’s actually very simple, check out recipe 31 from the Rails Recipes  
book: Getting Notified of Unhandled Exceptions. It also elaborates on  
how to get notified by mail if an exception occurs.

Basically, you need to override the rescue\_action\_in\_public method  
from ActionController.

Best regards

Peter De Berdt
