From: Robert Klemme Date: 2005-10-08T00:31:50+09:00 Subject: Re: Customizing Exception, but only when an error is raised Berger, Daniel wrote: >> -----Original Message----- >> From: Robert Klemme [mailto:bob.news@gmx.net] >> Sent: Friday, October 07, 2005 6:52 AM >> To: ruby-talk ML >> Subject: Re: Customizing Exception, but only when an error is raised > > > >> It would be interesting to learn what real world problem >> Daniel wants to solve... >> >> Kind regards >> >> robert > > I was trying to emulate Perl's $SIG{__DIE__} handler for reports > running via cron where, whenever an error occurred, an email would be > sent to me (or whoever). I'm not too familiar with $SIG{__DIE__} but I assume it's called whenever the program executes a die. If this is the case, there are several alternatives in Ruby: 1. wrap your complete script in begin;rescue;end and handle the exception appropriately at this top level. 1b. like 1 but catch only SystemExit 2. You can use at_exit to set an exit handler http://www.ruby-doc.org/core/classes/Process.html#M001763 3. Use Signal.trap http://www.ruby-doc.org/core/classes/Signal.html#M001593 4. In certain circumstances you might be able to use END {} > While I can log errors to a file, I still > have to check the log files periodically to make sure they ran as > expected. Every once in a while something goes awry - network > glitch, database glitch - whatever. > > No, I don't want to run a separate program to tail log files looking > for errors. :) > > My initial plan (which I had nearly complete) was to do something like > this: > > # At the top of your program > require "exception/mail" > Exception.mail_host = "mailhost.foo.com" > Exception.mail_to = "person@foo.com" > > Then, whenever an error was raised anywhere in my program an email > would be sent out. There are plenty of other options you could > configure, btw, though the others all have reasonable defaults. > > The problem, I realized, was that even if I wanted to ignore an error > (or handle it some other way), an email would still be sent out. Now > I'm thinking that maybe I should redefine Kernel#raise instead. I would refrain from that and use one of the other approaches as fiddling with such core features might lead to surprising effects (for example, if you alias this method but 0/0 doesn't even invoke it but raises exceptions differently) and incompatibilities. Kind regards robert