[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

if expression or muffle an exception?

aidy

7/26/2008 2:03:00 PM

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
3 Answers

Phlip

7/26/2008 2:06:00 PM

0

aidy wrote:

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

--
Phlip

aidy

7/26/2008 2:28:00 PM

0

On Jul 26, 3:05 pm, Phlip <phlip2...@gmail.com> wrote:
> aidy wrote:

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

Good point


Aidy

Joel VanderWerf

7/26/2008 6:10:00 PM

0

aidy wrote:
> 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
*

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407