Search code examples
pythonpython-3.xvisual-studio-2013ptvs

PTVS in VS2013: Why does this for loop not catch the StopIteration?


I'm using PTVS with Python 3.4.1 in VS2013.

The following code tries to read past the end of the file and throws a StopIteration exception in VS2013. It runs fine in the Python interpreter.

source.csv is a simple csv file saved as a csv from Excel 2013.

csvreader.py:

import csv

with open(r"c:\data\source.csv", "r") as reader:
    csvreader = csv.DictReader(f=reader, dialect = 'excel')
    for row in csvreader:
        print(row)

source.csv:

Col1,Col2,Col3
a1,a2,a3
b1,b2,b3

Solution

  • StopIteration is raised there regardless, it's just that VS actually shows you the point at which it is raised. It doesn't mean that exception is unhandled - if you continue running from that point, you'll see that it works exactly the same.

    Now, it shouldn't report this exception by default (the default setting for it in Debug -> Exceptions is report unhandled, so it should only be reported if it's actually unhandled). If you're using PTVS 2.0, then most likely you're hitting this bug. I would recommend switching to 2.1 beta, which fixes this among many other things.