From: Gary Wright Date: 2010-11-30T05:30:49+09:00 Subject: Re: when using system(), how do I redirect Exception msgs? 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? 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