# If expression or muffle an exception?

**URL:** <https://www.ruby-forum.com/t/if-expression-or-muffle-an-exception/144195>\
**Category:** Ruby\
**Created:** [July 26, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/if-expression-or-muffle-an-exception/144195 "2008-07-26T16:05:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 26, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/if-expression-or-muffle-an-exception/144195/1 "2008-07-26T16:05:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 26, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/if-expression-or-muffle-an-exception/144195/2 "2008-07-26T16:11:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 26, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/if-expression-or-muffle-an-exception/144195/3 "2008-07-26T16:30:36Z")

</div>

On Jul 26, 3:05 pm, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 26, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/if-expression-or-muffle-an-exception/144195/4 "2008-07-26T20:17:15Z")

</div>

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
     *
```
