# Why can't I catch this exception?

**URL:** <https://www.ruby-forum.com/t/why-cant-i-catch-this-exception/101496>\
**Category:** Ruby\
**Created:** [August 5, 2006, 10:59am UTC](https://www.ruby-forum.com/t/why-cant-i-catch-this-exception/101496 "2006-08-05T10:59:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [August 5, 2006, 10:59am UTC](https://www.ruby-forum.com/t/why-cant-i-catch-this-exception/101496/1 "2006-08-05T10:59:55Z")

</div>

(this is in the context of Rails, buts its primarily a Ruby question  
dealing with exception handling)

This is driving me nuts - I’m tracking down a bug in ActionPack that  
causes rails to do a 500 error with no info, or when testing causes an  
immediate exit with no information or stack trace.

I’ve tracked it down to ActionView::TemplateError, where numerous  
things can happen that cause an exception to be thrown from w/o the  
TemplateError. For instance, the following controller will cause the  
error to happen:

class FooController \< ApplicationController  
def bad\_file\_with\_full\_path\_false  
render :file =\> “unknown”, :use\_full\_path =\> false  
end  
end

And I cannot get the exception to be caught in a test case, no matter  
what I do. For example, w/i a standard FooControllerTest:

def test\_bad\_file\_render\_with\_full\_path\_false  
assert\_raises ActionController::MissingTemplate do  
get :bad\_file\_with\_full\_path\_false  
end  
rescue Exception =\> e  
flunk(“failed”)  
end

This just exits - no flunk msg, assert\_raise doesn’t catch, nothing.  
I also commented out an “ensure” in ActionController::Base#process, to  
make sure that wouldn’t halt the exception. It still exits.

I tracked it down into TemplateError via a lot of tedious logging, and  
it seems the line here throws an error (though I cant figure out why,  
it works in irb):

[http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action\_view/template\_error.rb#L79](http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_view/template_error.rb#L79)

and this line can also throw an exception, which makes sense because  
@file\_name refers to a non-existent file:

[http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action\_view/template\_error.rb#L29](http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_view/template_error.rb#L29)

And of course none of that answers why I can’t rescue the exception  
back in my calling code, no matter what I do. Any ideas?

- thanks,
- Rob

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [August 5, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/why-cant-i-catch-this-exception/101496/2 "2006-08-05T18:06:18Z")

</div>

On 8/5/06, Rob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This just exits - no flunk msg, assert\_raise doesn’t catch, nothing.

If it _is_ raising the exception, you won’t reach that rescue –  
#assert\_raises rescues the exception internally. Perhaps you meant to  
use #assert\_nothing\_raised ?

–  
Regards,  
John W.

> **[John Wilger](http://johnwilger.com)**
>
> The personal website of John Wilger; Dad, Cyclist, Software Developer

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [August 5, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/why-cant-i-catch-this-exception/101496/3 "2006-08-05T21:30:31Z")

</div>

On 8/5/06, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> > 
> > This just exits - no flunk msg, assert\_raise doesn’t catch, nothing.
> 
> If it _is_ raising the exception, you won’t reach that rescue –  
> #assert\_raises rescues the exception internally. Perhaps you meant to  
> use #assert\_nothing\_raised ?
> 
> –  
> Regards,  
> John W.

John,  
I realize assert\_raises _should_ rescue it, but then it should either  
pass or fail based on the class of the exception. My main issue is  
that the program just exits, with out success or failure for that test  
class. So its basically as if it was calling exit (which its not).

Doing this will also show the same behavior:

def test\_bad\_file\_render\_with\_full\_path\_false  
get :bad\_file\_with\_full\_path\_false  
end

So my question is, what is happening inside TemplateError that it  
causes the whole process to do a hard exit?

- Rob

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [August 5, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/why-cant-i-catch-this-exception/101496/4 "2006-08-05T22:12:34Z")

</div>

On 8/5/06, Rob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I realize assert\_raises _should_ rescue it, but then it should either  
> pass or fail based on the class of the exception. My main issue is  
> that the program just exits, with out success or failure for that test  
> class. So its basically as if it was calling exit (which its not).

Ah, OK. I misunderstood and simply thought that you had a test that  
was passing when you expected it to fail.

I tried out your example on my OSX system (ruby 1.8.4, compiled form  
src). I had the same result both with the latest release of Rails and  
after freezing to edge Rails. Not much help, I know – but it’s not  
just you, FWIW.

–  
Regards,  
John W.

> **[John Wilger](http://johnwilger.com)**
>
> The personal website of John Wilger; Dad, Cyclist, Software Developer

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [August 5, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/why-cant-i-catch-this-exception/101496/5 "2006-08-05T22:18:35Z")

</div>

On 8/5/06, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> src). I had the same result both with the latest release of Rails and  
> after freezing to edge Rails. Not much help, I know – but it’s not  
> just you, FWIW.

Thanks for confirming that I’m not going crazy =). I understand that  
it should error out for a bad file (which I’m going to submit a patch  
for), but I don’t understand what kind of error would cause it to just  
crash like that w/o a “rescuable” exception.

- rob
