[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Detect errors in fuctions



Marcos wrote:
Is similar with python & pygame? Now if a do: "pygame.image.load("./imagethatnotexist")"
anytime SDL gives an error, it is turned into a python exception. a great thing about SDL is that all errors come with good descriptions, and these are also included in the exception. some examples?

try:
win = pygame.display.set_mode((x,y), flags, bits)
except pygame.error, reason:
print 'Couldn't init graphics'
print reason
sys.exit(1)

try:
name = 'missingimage.png'
img = pygame.image.load()
except pygame.error, reason:
print 'Could not load image', name
print reason



sometimes there is sort of a 'grey area' about what type of exception is going to be raised. for the 'imagethatnotexist' example, should you get a standard python IOError, or the pygame.error exception? for the most part, if the error happens inside SDL code you get the pygame.error. if the error happens inside pygame code it often gives one of the regular python exceptions.