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.
>>> 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.


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.


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

Kickstart Your Career

Get certified by completing the course

Get Started