title: Try except not catching IOError from class
body: <p>I have a class that reads a file of a particular format. These files tend to be greater than 8Gb in size so are usually compressed. When reading the file in I wanted to catch the error of the file not being compressed but neither <code>except IOError:</code> nor <code>except:</code> will do so, for some reason I don't understand.</p>
  
  <p>There are a few classes defined together in the file <strong>VCF.py</strong>, though the offending class is <code>vcfReader()</code>. The file from which the object is instantiated is below <strong>test.py</strong>, and lastly the Traceback.</p>
  
  <p>Anyone have any ideas as to why it isn't working?</p>
  
  <p><strong>VCF.py</strong></p>
  
  <pre><code>import gzip
  import sys
  
  class Call():
      '''
      Class to handle the sample genotypes and associated information
      '''
  
      def __init__(self,site,sample,format,data):
          #do stuff here#
  
  class Variant():
      '''
      Class for a single row from a VCF file.
      '''
      def __init__(self, entry, samples):
         #do other stuff here
  
  
  class vcfReader():
      '''
      read a compressed vcf file ignoring the meta-information, but parsing the header             for sample names
      '''  
      def __init__(self, file):
          try:
              self.vcfFile = gzip.open(file, 'rb')
          except IOError:
              print "Not a gzipped file"
              sys.exit()
  
          self.samples = self.readHeader()
  
      def readHeader(self):
          line = self.vcfFile.next()
          while line.startswith('#'):
              if line[1]!='#':
                  #lines that start with ##, i.e. meta tags are ignored. Header line starting with '#', sample names are extracted.
                  return line.rstrip().rsplit('\t')[9:]
              else:           
                  line = self.vcfFile.next()
  
      def __iter__(self):
          return self
  
      def next(self):
          row =  self.vcfFile.next()
          return Variant(row, self.samples)
  </code></pre>
  
  <p>and then <strong>test.py</strong></p>
  
  <pre><code>import VCF
  from collections import Counter
  
  if __name__=='__main__':
      vcfreader = VCF.vcfReader('all_samples.vcf')
  
      filters = []
      for i in vcfreader:
          filters.extend(i.FILTERS)
  
      filters = Counter(filters)
  
      for k,v in filters.iteritems():
          print "{0}: {1}".format(k,v)
  </code></pre>
  
  <p>Here is the traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Davy\Documents\Programming\VCF_stuff\src\test.py", line 10, in &lt;module&gt;
      vcfreader = VCF.vcfReader('all_samples.vcf')
    File "C:\Users\Davy\Documents\Programming\VCF_stuff\src\VCF.py", line 95, in __init__
      self.samples = self.readHeader()
    File "C:\Users\Davy\Documents\Programming\VCF_stuff\src\VCF.py", line 98, in readHeader
      line = self.vcfFile.next()
    File "C:\Python27\lib\gzip.py", line 450, in readline
      c = self.read(readsize)
    File "C:\Python27\lib\gzip.py", line 256, in read
      self._read(readsize)
    File "C:\Python27\lib\gzip.py", line 291, in _read
      self._read_gzip_header()
    File "C:\Python27\lib\gzip.py", line 185, in _read_gzip_header
      raise IOError, 'Not a gzipped file'
  IOError: Not a gzipped file
  </code></pre>