9.12 Errors and Exceptions

9.12.1 Module errorcode
9.12.2 Exception errors.Error
9.12.3 Exception errors.DataError
9.12.4 Exception errors.DatabaseError
9.12.5 Exception errors.IntegrityError
9.12.6 Exception errors.InterfaceError
9.12.7 Exception errors.InternalError
9.12.8 Exception errors.NotSupportedError
9.12.9 Exception errors.OperationalError
9.12.10 Exception errors.PoolError
9.12.11 Exception errors.ProgrammingError
9.12.12 Exception errors.Warning
9.12.13 Function errors.custom_error_exception(error=None, exception=None)

The mysql.connector.errors module defines exception classes for errors and warnings raised by MySQL Connector/Python. Most classes defined in this module are available when you import mysql.connector.

The exception classes defined in this module mostly follow the Python Database API Specification v2.0 (PEP 249). For some MySQL client or server errors it is not always clear which exception to raise. It is good to discuss whether an error should be reclassified by opening a bug report.

MySQL Server errors are mapped with Python exception based on their SQLSTATE value (see Server Error Codes and Messages). The following table shows the SQLSTATE classes and the exception Connector/Python raises. It is, however, possible to redefine which exception is raised for each server error. Note that the default exception is DatabaseError.

Table 9.1 

SQLSTATE ClassConnector/Python Exception
02DataError
02DataError
07DatabaseError
08OperationalError
0ANotSupportedError
21DataError
22DataError
23IntegrityError
24ProgrammingError
25ProgrammingError
26ProgrammingError
27ProgrammingError
28ProgrammingError
2AProgrammingError
2BDatabaseError
2CProgrammingError
2DDatabaseError
2EDatabaseError
33DatabaseError
34ProgrammingError
35ProgrammingError
37ProgrammingError
3CProgrammingError
3DProgrammingError
3FProgrammingError
40InternalError
42ProgrammingError
44InternalError
HZOperationalError
XAIntegrityError
0KOperationalError
HYDatabaseError