I always get stuck with errors when building scripts. Sometimes I want the program to do something even if there is an error. Here is an example of a simple way to catch errors:
try:
file = h5py.File(file.h5, 'r')
print 'Yes, this file exists'
except:
print 'Didn't work'
sys.exit()
Here I try to open the HDF5 file called 'file.h5'. If the file exists we print "Yes, this file exists."
If there is an error when the computer tries to open the file--for example, the file can not be found--then we print "Didn't work" and the python program closes.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.