if expression or muffle an exception?

A

aidy

Hi,

I am wondering if this is bad form,

begin
system("taskkill /im firefox.exe /f")
rescue
#preventing exception if firefox process does not exist
end

or should I use an if expression?

Thank You

Aidy
 
P

Phlip

aidy said:
I am wondering if this is bad form,

begin
system("taskkill /im firefox.exe /f")
rescue
#preventing exception if firefox process does not exist
end

or should I use an if expression?

You can't use an if (such as if `pgrep firefox.exe` =~ /\d+/) because
firefox.exe might disappear in the split second between the if and the system.

That's the main difference between things you can if and things you must rescue
- times when the if would have the wrong side-effects.
 
J

Joel VanderWerf

aidy said:
begin
system("taskkill /im firefox.exe /f")
rescue
#preventing exception if firefox process does not exist
end

Kernel#system doesn't raise exceptions. Check the return value and $?.

---------------------------------------------------------- Kernel#system
system(cmd [, arg, ...]) => true or false
------------------------------------------------------------------------
Executes _cmd_ in a subshell, returning +true+ if the command was
found and ran successfully, +false+ otherwise. An error status is
available in +$?+. The arguments are processed in the same way as
for +Kernel::exec+.

system("echo *")
system("echo", "*")

_produces:_

config.h main.rb
*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top