Pygame - Errors and Exception



Top level pygame module defines pygame.error as a standard Pygame exception. This exception is raised whenever a pygame or SDL operation fails. You can catch any anticipated problems and deal with the error. The exception is always raised with a descriptive message about the problem.

>>> import pygame
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> screen = pygame.display.set_mode((640, -1))
Traceback (most recent call last):
   File "<pyshell#1>", line 1, in <module>
      screen = pygame.display.set_mode((640, -1))
pygame.error: Cannot set negative sized display mode

Being derived from the RuntimeError exception, which can also be used to catch these raised errors.

>>> try:
   screen = pygame.display.set_mode((640, -1))
except pygame.error as e:
   print ("unable to set display: ", e)

unable to set display Cannot set: negative sized display mode

There are two more functions in this module to set and retrieve error message.

set_error(error_msg)

SDL maintains an internal error message. When pygame.error()standard pygame exception is raised, this string is used as error message.

It gets the current error message.

get_error()

It returns the string as error message of pygame.error() message.

Advertisements