[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Exception catch-all

Leslie Viljoen

11/8/2006 11:14:00 AM

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

5 Answers

Paul Lutus

11/8/2006 11:42:00 AM

0

Leslie Viljoen 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.

--
Paul Lutus
http://www.ara...

Jano Svitok

11/8/2006 1:45:00 PM

0

On 11/8/06, Paul Lutus <nospam@nosite.zzz> wrote:
> Leslie Viljoen 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.

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.

Leslie Viljoen

11/8/2006 2:15:00 PM

0

On 11/8/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 11/8/06, Paul Lutus <nospam@nosite.zzz> wrote:
> > Leslie Viljoen 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.
>
> 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.

David Vallner

11/8/2006 11:07:00 PM

0

Leslie Viljoen 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 Vallner

Roger Pack

10/1/2007 6:29:00 PM

0


>> 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
--
Posted via http://www.ruby-....