how to trap the value error in python
Question: If any file is corrupted and cannot be open, the following code raise value
error and stop opening of further files. How can I print out the name of the
file which raised value error, and make the program to run further to detect
and print out next file which raise value error?

    
    
    import glob
    from PIL import Image
    files = glob.glob('*.jpg')
    for f in files:
        im = Image.open(f)
        print im
    


Answer: You can _catch_ the exception with a `try:` / `except:` handler:

    
    
    for f in files:
        try:
            im = Image.open(f)
        except IOError as e:
            print '{} failed to load: {}'.format(f, e)
        else:
            print im
    

The Python tutorial has a [chapter devoted to error
handling](http://docs.python.org/2/tutorial/errors.html) you might want to
read.

