# Error handling in Ruby scripts

**URL:** <https://www.ruby-forum.com/t/error-handling-in-ruby-scripts/160197>\
**Category:** Ruby\
**Created:** [February 12, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/error-handling-in-ruby-scripts/160197 "2009-02-12T20:40:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 12, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/error-handling-in-ruby-scripts/160197/1 "2009-02-12T20:40:09Z")

</div>

Hi,  
Can someone please help me with exception handling? I’ve written some  
classes and scripts that I’ve used for a year or two. But now, I’m  
having to use them with many thousands of files at once, and, even  
though they process just one file at a time, there are sometimes errors  
in the tools that I use. Specifically, I use an image processing tool  
called Image Alchemy. In my Ruby scripts, I have a class written that  
has in it various conversion routines where it converts the file pointed  
to in Ruby to various formats–TIFF, PNG, etc., etc. My problem is that  
when I’m presented with many thousands of files, I’m getting the odd  
input file that’s just bad. So, my Alchemy Ruby code fails, because  
Alchemy itself is failing with that bad file. So, I’ve read a bit about  
“raise,” but, I can’t seem to get it to work. Can I use “raise” as an  
exception handler in my class? Then, in my scripts, could I use  
“begin/rescue” with that exception?

Below is a subset of my class

Thanks,  
Peter

class Alchemy  
def initialize(file)  
@file = file  
end  
def Alchemy.tiff(file)  
`alchemy #{file} -t1 -Zc1 -Zm2 -Za4 -Zd 300 300 .300 -o`  
end  
def Alchemy.tiffbw(file)  
`alchemy #{file} -t204 -Zc1 -Zm2 -Za4 -Zd 600 600 .300 -o`  
end  
def Alchemy.tiffgray(file)  
`alchemy #{file} -t1 -Zc1 -Zm2 -b -c256 -8 -Za4 -Zd 300 300 .300 -o`  
end  
end

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 12, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/error-handling-in-ruby-scripts/160197/2 "2009-02-12T21:51:50Z")

</div>

On Feb 12, 1:38 pm, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## input file that’s just bad. So, my Alchemy Ruby code fails, because class Alchemy `alchemy #{file} -t1 -Zc1 -Zm2 -b -c256 -8 -Za4 -Zd 300 300 .300 -o` end end
> 
> Posted viahttp://www.ruby-forum.com/.

its not going to work the way you are doing it. backquotes cant tell  
if the command inside them completed “successfully” or not, they just  
run it and return STDOUT. you need to analyze the return status of the  
command, then raise an error if it failed.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 14, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/error-handling-in-ruby-scripts/160197/3 "2009-02-14T18:11:31Z")

</div>

On Fri, Feb 13, 2009 at 2:30 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > its not going to work the way you are doing it. backquotes cant tell

You can get the exit status from $?.exitstatus

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 16, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/error-handling-in-ruby-scripts/160197/4 "2009-02-16T23:27:06Z")

</div>

Andrew T. wrote:

> On Fri, Feb 13, 2009 at 2:30 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > its not going to work the way you are doing it. backquotes cant tell
> 
> You can get the exit status from $?.exitstatus
> 
> Andrew T.  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> “I have never let my schooling interfere with my education” - Mark Twain

Thank you. I’ll try that.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 13, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/error-handling-in-ruby-scripts/160197/5 "2009-02-13T13:32:08Z")

</div>

snex wrote:

> On Feb 12, 1:38ï¿½pm, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## input file that’s just bad. So, my Alchemy Ruby code fails, because class Alchemy ï¿½ ï¿½ `alchemy #{file} -t1 -Zc1 -Zm2 -b -c256 -8 -Za4 -Zd 300 300 .300 -o` ï¿½ end end
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> its not going to work the way you are doing it. backquotes cant tell  
> if the command inside them completed “successfully” or not, they just  
> run it and return STDOUT. you need to analyze the return status of the  
> command, then raise an error if it failed.

OK. Thanks. Well, obviously, I’ve got to use backquotes or “system” in  
this case. Being a Windows command shell program, a successful result  
would be “0” in Windows, “1” or anything else if unsuccessful. Can I  
capture that as part of my “raise?”
