# Handle a special foramt error when importing csv

**URL:** <https://www.ruby-forum.com/t/handle-a-special-foramt-error-when-importing-csv/122583>\
**Category:** Ruby\
**Created:** [December 9, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/handle-a-special-foramt-error-when-importing-csv/122583 "2007-12-09T21:03:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 9, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/handle-a-special-foramt-error-when-importing-csv/122583/1 "2007-12-09T21:03:24Z")

</div>

hello,

I have to import lots of csv files, and sometimes, there’s one with the  
.csv that is not quite good formatted, and Ruby send a:

CSV::IllegalFormatError in DadeController#import\_file\_data\_ajax  
CSV::IllegalFormatError

I’m trying to handle the error with

begin  
# Exceptions raised by this code will  
# be caught by the following rescue clause

… some code …

rescue Exception  
$stderr.print "IO failed: " + $!

```
   ... some code ...

  raise

```

but the error/exception never raises …

I tried with SystemCAllError, StandardError, Exception, …

The error happens in:  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:639:in  
`get\_row’

because the file hasn’t the expected formatted, it’s not a csv file, but  
the file has an extension of csv, that’s why I want to catch those  
exceptions …

thanks,

regards

raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 9, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/handle-a-special-foramt-error-when-importing-csv/122583/2 "2007-12-09T23:06:23Z")

</div>

Hi,

On Dec 9, 2007 3:03 PM, Raimon Fs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> begin  
> # Exceptions raised by this code will  
> # be caught by the following rescue clause
> 
> … some code …
> 
> rescue Exception

try this,  
rescue CSV::IllegalFormatError

> regards
> 
> ## raimon
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

HTH,  
Michael G.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 9, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/handle-a-special-foramt-error-when-importing-csv/122583/3 "2007-12-09T23:46:52Z")

</div>

Michael G. wrote:

> Hi,
> 
> On Dec 9, 2007 3:03 PM, Raimon Fs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > begin  
> > # Exceptions raised by this code will  
> > # be caught by the following rescue clause
> > 
> > … some code …
> > 
> > rescue Exception
> 
> try this,  
> rescue CSV::IllegalFormatError

thanks, I’ll try that …

I wasn’t aware that I could simply catch those errors, beacuse I would  
like a more general error, because I don’t know in advance how many  
errors I’m going to find …

regards,

rai
