Global Exception Rescue

I need to run a script in a special environment. I need to change
how STDIN works, for example, to fetch responses from GUI. That’s
easily done and can be made to work via a simple:

ruby -r my_special_env script_to_run.rb

However, one element keeps tripping me up. My special environment
must also catch exceptions not handled by the script, for error
reporting purposes. Is there any way to do the via a require?

The best idea I have so far is to add a begin … rescue … end around
the code in the script and then execute it, but this seems mighty
clumsy when you take into account things like the working directory,
the script name, and line number offsets. It’s doable, but I would
love to find a more elegant solution.

Any ideas you can provide are appreciated.

James Edward G. II

2006/6/23, James Edward G. II [email protected]:

The best idea I have so far is to add a begin … rescue … end around
the code in the script and then execute it, but this seems mighty
clumsy when you take into account things like the working directory,
the script name, and line number offsets. It’s doable, but I would
love to find a more elegant solution.

Any ideas you can provide are appreciated.

James, I have only ideas ATM:

  • use at_exit
  • use END

Maybe that helps

robert

On Jun 23, 2006, at 10:25 AM, Mat S. wrote:

Would this be unacceptable? It seems to me you could turn your
environment into an app much like rcov or ruby-prof.

This is what I’ve been using, that had to be replaced. :frowning: The
problems here are many: DATA, $0/$PROGRAM_NAME, etc.

James Edward G. II

On Jun 23, 2006, at 10:35 AM, James Edward G. II wrote:

The best idea I have so far is to add a begin … rescue … end
around the code in the script and then execute it, but this seems
mighty clumsy when you take into account things like the working
directory, the script name, and line number offsets. It’s doable,
but I would love to find a more elegant solution.

Any ideas you can provide are appreciated.

James Edward G. II

What about something like:
begin
load(ARGV[0])
rescue Exception => e
do_stuff_with e
end

then:
ruby my_special_env.rb script_to_run.rb

Would this be unacceptable? It seems to me you could turn your
environment into an app much like rcov or ruby-prof.
-Mat

On Jun 23, 2006, at 11:07 AM, [email protected] wrote:

a.rb:2: foobar (RuntimeError)

regards.

You are my hero! Thank you so much.

James Edward G. II

On Fri, 23 Jun 2006, James Edward G. II wrote:

The best idea I have so far is to add a begin … rescue … end around the
code in the script and then execute it, but this seems mighty clumsy when
you take into account things like the working directory, the script name,
and line number offsets. It’s doable, but I would love to find a more
elegant solution.

Any ideas you can provide are appreciated.

James Edward G. II

 harp:~ > cat env.rb
 STDIN.reopen(open(__FILE__))
 at_exit do
   e = $!
   if e
     require 'logger'
     Logger.new(STDERR).fatal{ e }
   end
 end


 harp:~ > cat a.rb
 puts gets
 raise 'foobar'


 harp:~ > ruby -r env.rb a.rb
 STDIN.reopen(open(__FILE__))
 F, [2006-06-23T10:04:35.279359 #15389] FATAL -- : foobar 

(RuntimeError)
a.rb:2
a.rb:2: foobar (RuntimeError)

note that the original behaviour (dumping to STDERR) is also done. if
you
don’t want this add in

at_exit do

exit!
end

regards.

-a

On Jun 23, 2006, at 12:07 PM, [email protected] wrote:

a.rb:2

end

regards.

-a

Can you explain why you have STDIN.reopen(open(FILE)) there?
It’s really baffling me. I can’t figure out just what it’s doing.
-Mat

On Sat, 24 Jun 2006, Mat S. wrote:

Can you explain why you have STDIN.reopen(open(FILE)) there?
It’s really baffling me. I can’t figure out just what it’s doing.
-Mat

james had said in the original message that his ‘wrapper’ was doing some
things like munging STDIN - this is simply doing that: in this case
STDIN is
redirected to read the file itself. it could just as well be

STDIN.reopen(open(‘alternate.stdin’))

regards.

-a

James Edward G. II [email protected] writes:

On Jun 23, 2006, at 9:48 AM, Robert K. wrote:

James, I have only ideas ATM:

  • use at_exit
  • use END

Is there any way for me to tell, at the time these are invoked, that
the code is exiting under exceptional conditions?

IIRC, $! is set.

On Jun 23, 2006, at 9:48 AM, Robert K. wrote:

James, I have only ideas ATM:

  • use at_exit
  • use END

Is there any way for me to tell, at the time these are invoked, that
the code is exiting under exceptional conditions?

James Edward G. II