Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions breaks iteration loops. #39

Open
kilsbo opened this issue Feb 23, 2015 · 2 comments
Open

Exceptions breaks iteration loops. #39

kilsbo opened this issue Feb 23, 2015 · 2 comments

Comments

@kilsbo
Copy link

kilsbo commented Feb 23, 2015

I am using the following code:

      val it = CSVReader.open(file).iterator
      val line = it.next
      println("format: " + line)

      var cont = true

      while (it.hasNext) {

        try {
          val line = it.next

          //println(line)

        }
        catch {
          case e: Exception => {
            println("Line failed")
            println(e.getMessage)
            errcnt += 1
          }
        }
      }

When the iterator hits a malformed CSV-line, it throws an Exception. However, it does it already on the it.hasNext call, making it impossible for me to skip that line and continue.

I tried to use a boolean in the where-clause, but it seems that the faulty line breaks the iterator so I cannot use that approach either.

Are there other ways to do this, or do we need a fix here?

@kilsbo kilsbo changed the title Exceoptions breaks iteration loops. Exceptions breaks iteration loops. Feb 23, 2015
@kilsbo
Copy link
Author

kilsbo commented Feb 23, 2015

Ok so I was wrong on one thing, it does continue after a failed it.hasNext, however, would like for it to not throw an exception on it.hasNext, but on it.next.

@mossprescott
Copy link

I'm believe I'm seeing another manifestation of this, which is that toStream doesn't behave consistently when parse failures occur. The first attempt to read the stream throws an exception, and then subsequent attempts behave as if the stream just ended before the bad record.

For example:

scala> import com.github.tototoshi.csv._
import com.github.tototoshi.csv._

scala> val s = CSVReader.open(new java.io.StringReader("a,b\n1,2\n,\"\n")).toStream
s: Stream[List[String]] = Stream(List(a, b), ?)

scala> s.toList
com.github.tototoshi.csv.MalformedCSVException: Malformed Input!: Some(,"
)
  at com.github.tototoshi.csv.CSVReader.parseNext$1(CSVReader.scala:39)

scala> s.toList
res1: List[List[String]] = List(List(a, b), List(1, 2))

I'm not sure if it's generally acceptable for Streams to throw exceptions, but certainly it seems like it should do so consistently. Alternatively, return Either[SomeError, List[String]] or use a different lazy sequence that supports failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants