When using system(), how do I redirect Exception msgs?

So from my console, I execute the Ruby script via:

ruby TestSuite.rb > /Users/dave/Desktop/outputtext.xml

Within my script I have several

···

---
begin
system('ruby /Users/dave/Desktop/testruby.rb');
system('ruby /Users/dave/Desktop/testruby2.rb');
system('ruby /Users/dave/Desktop/testruby3.rb');
rescue Exception=>msg
puts("Hi there exception")
puts(msg)
end
---

calls...

To test it, I put in testruby4.rb, but no file called testruby4.rb
exists.

I get a LoadError Exception as expected, and in my script I see the
trace on the console, but not via the puts() commands...

How come the script is ignoring the rescue clause I have here and how
can I get it to not ignore it?

THanks!

--
Posted via http://www.ruby-forum.com/.

David E. wrote in post #964857:

So from my console, I execute the Ruby script via:

ruby TestSuite.rb > /Users/dave/Desktop/outputtext.xml

Within my script I have several
---
begin
system('ruby /Users/dave/Desktop/testruby.rb');
system('ruby /Users/dave/Desktop/testruby2.rb');
system('ruby /Users/dave/Desktop/testruby3.rb');
rescue Exception=>msg
puts("Hi there exception")
puts(msg)
end
---

calls...

To test it, I put in testruby4.rb, but no file called testruby4.rb
exists.

I get a LoadError Exception as expected, and in my script I see the
trace on the console, but not via the puts() commands...

How come the script is ignoring the rescue clause I have here and how
can I get it to not ignore it?

THanks!

I answered/found my own question.

Instead of system(), I use load(). This seems to run the script within
the "Calling script" context. I'll figure out more exactly why, but it
seems as if load() works.

···

--
Posted via http://www.ruby-forum.com/\.

The exception is occurring in the child ruby process started by 'system' and not in your TestSuite.rb ruby process. Ruby exceptions don't propagate from a child process to the parent. You need to capture the return value from 'system' to determine in the child process exited cleanly or not. If the process started by 'system' fails, 'system' will return false:

ruby-1.9.2-p0 > if system 'ruby foobar.rb'
ruby-1.9.2-p0 ?> puts 'ok'
ruby-1.9.2-p0 ?> else
ruby-1.9.2-p0 > puts 'failed'
ruby-1.9.2-p0 ?> end
ruby: No such file or directory -- foobar.rb (LoadError)
failed
=> nil

Gary Wright

···

On Nov 29, 2010, at 3:14 PM, David E. wrote:

So from my console, I execute the Ruby script via:

ruby TestSuite.rb > /Users/dave/Desktop/outputtext.xml

Within my script I have several
---
begin
system('ruby /Users/dave/Desktop/testruby.rb');
system('ruby /Users/dave/Desktop/testruby2.rb');
system('ruby /Users/dave/Desktop/testruby3.rb');
rescue Exception=>msg
puts("Hi there exception")
puts(msg)
end
---

calls...

To test it, I put in testruby4.rb, but no file called testruby4.rb
exists.

I get a LoadError Exception as expected, and in my script I see the
trace on the console, but not via the puts() commands...

How come the script is ignoring the rescue clause I have here and how
can I get it to not ignore it?

You're right. Is there a better way to run the ruby script within a ruby
script and get the full output?

···

--
Posted via http://www.ruby-forum.com/.

Just be aware that you won't have very good isolation between your
scripts if you use load() since everything will be happening in the
context of a single Ruby process. That may or may not be OK depending
on what you are doing.

Gary Wright

···

On Nov 29, 2010, at 3:24 PM, David E. wrote:

I answered/found my own question.

Instead of system(), I use load(). This seems to run the script within
the "Calling script" context. I'll figure out more exactly why, but it
seems as if load() works.

Ah... YOu're right. Is there a way to get all other values from the
system process? (such as the exception trace).

···

--
Posted via http://www.ruby-forum.com/.

I think you might find the open4 gem helpful:

<http://rubydoc.info/gems/open4/1.0.1/frames&gt;

Gary Wright

···

On Nov 29, 2010, at 3:40 PM, David E. wrote:

Ah... YOu're right. Is there a way to get all other values from the
system process? (such as the exception trace).

I think what you may want to do instead is continue using the load
method and set its optional second parameter to true for each call.
According to the documentation, that will run the file under an
anonymous module and protect your global namespace:

http://ruby-doc.org/core/classes/Kernel.html#M005940

-Jeremy

···

On 11/29/2010 2:40 PM, David E. wrote:

Ah... YOu're right. Is there a way to get all other values from the
system process? (such as the exception trace).