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.

(pygmenv) D:\Projects\python\pygmenv>py
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.6.1 (SDL 2.28.4, Python 3.12.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> screen = pygame.display.set_mode((640, -1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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