# Exception catch-all

**URL:** <https://www.ruby-forum.com/t/exception-catch-all/75235>\
**Category:** Ruby\
**Created:** [November 8, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/exception-catch-all/75235 "2006-11-08T12:14:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [November 8, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/exception-catch-all/75235/1 "2006-11-08T12:14:39Z")

</div>

I have a script that needs to run unattended every night, and I need  
to log if it throws an exception anywhere. Is this as simple as  
putting a begin…rescue…end around the top-most statements? I know  
that doesn’t work in C#. Is there a hook or some way that a catch-all  
is supposed to be done in Ruby?

Les

---

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [November 8, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/exception-catch-all/75235/2 "2006-11-08T12:46:46Z")

</div>

Leslie V. wrote:

> I have a script that needs to run unattended every night, and I need  
> to log if it throws an exception anywhere. Is this as simple as  
> putting a begin…rescue…end around the top-most statements? I know  
> that doesn’t work in C#. Is there a hook or some way that a catch-all  
> is supposed to be done in Ruby?
> 
> Les

This should catch everything, AFAIK:

begin  
…  
rescue Exception =\> err  
puts err  
end

If there is nothing but function calls between “begin” and “rescue”,  
e.g. if  
the entire executable block lies there, this should do it.

---

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [November 8, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/exception-catch-all/75235/3 "2006-11-08T15:15:17Z")

</div>

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

> > 
> 
> You should put this begin…rescue…end around each thread body you  
> start, unless you set  
> Thread.abort\_on\_exception = true. Otherwise exceptions in other  
> threads might get lost.
> 
> Finally I’ll mention that rescue without parameter rescues only  
> StandardError, and therefore doesn’t catch all Exceptions.

Thanks, that helps.

---

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [November 9, 2006, 12:06am UTC](https://www.ruby-forum.com/t/exception-catch-all/75235/4 "2006-11-09T00:06:54Z")

</div>

Leslie V. wrote:

> I know  
> that doesn’t work in C#.

Huh?

As far as I know, Ruby’s exception handling isn’t in any way  
revolutionary compared to C++ derivatives, so if that solution works in  
Ruby (as others recommended), it should work in C# too…

Also, that solution isn’t much different from just dropping the  
“exception handling” (which it really isn’t if you’re just catching them  
on toplevel) and redirecting standard output to a file. Of course, the  
point remains with the thread scenario, and this doesn’t apply if you  
need to log differently than just dump the trace someplace you’ll find  
it later.

David V.

---

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [November 8, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/exception-catch-all/75235/5 "2006-11-08T14:47:11Z")

</div>

On 11/8/06, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This should catch everything, AFAIK:
> 
> begin  
> …  
> rescue Exception =\> err  
> puts err  
> end
> 
> If there is nothing but function calls between “begin” and “rescue”, e.g. if  
> the entire executable block lies there, this should do it.

You should put this begin…rescue…end around each thread body you  
start, unless you set  
Thread.abort\_on\_exception = true. Otherwise exceptions in other  
threads might get lost.

Finally I’ll mention that rescue without parameter rescues only  
StandardError, and therefore doesn’t catch all Exceptions.

---

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [October 1, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/exception-catch-all/75235/6 "2007-10-01T20:29:07Z")

</div>

> > If there is nothing but function calls between “begin” and “rescue”, e.g. if  
> > the entire executable block lies there, this should do it.
> 
> You should put this begin…rescue…end around each thread body you  
> start, unless you set  
> Thread.abort\_on\_exception = true. Otherwise exceptions in other  
> threads might get lost.
> 
> Finally I’ll mention that rescue without parameter rescues only  
> StandardError, and therefore doesn’t catch all Exceptions.

If you need more verbose output on unhandled exceptions (i.e. yours says  
file1.rb:72  
file1.rb:70  
… 20 lines …  
file3.rb:70

and you want all the exception lines, i think this code snippet does the  
trick.

Thread.abort\_on\_exception = true # dangerous, I know  
class Thread  
alias init\_old initialize  
def initialize \*args, &block  
begin  
init\_old args, &block  
rescue Exception =\> detail  
print “rescued an uncaught exception!”  
print detail.backtrace.join("\n")  
end  
end  
end

-Roger
