Skip to content

Commit 9775ac3

Browse files
[3.11] gh-89018: Improve documentation of sqlite3 exceptions (GH-27645) (#93836)
- Order exceptions as in PEP 249 - Reword descriptions, so they match the current behaviour Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> (cherry picked from commit bb0b768) Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@protonmail.com>
1 parent cde0dad commit 9775ac3

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

Doc/library/sqlite3.rst

+50-18
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ Connection Objects
813813
*name*, and reopen *name* as an in-memory database based on the
814814
serialization contained in *data*. Deserialization will raise
815815
:exc:`OperationalError` if the database connection is currently involved
816-
in a read transaction or a backup operation. :exc:`DataError` will be
816+
in a read transaction or a backup operation. :exc:`OverflowError` will be
817817
raised if ``len(data)`` is larger than ``2**63 - 1``, and
818818
:exc:`DatabaseError` will be raised if *data* does not contain a valid
819819
SQLite database.
@@ -844,7 +844,7 @@ Cursor Objects
844844
:ref:`placeholders <sqlite3-placeholders>`.
845845

846846
:meth:`execute` will only execute a single SQL statement. If you try to execute
847-
more than one statement with it, it will raise a :exc:`.Warning`. Use
847+
more than one statement with it, it will raise a :exc:`ProgrammingError`. Use
848848
:meth:`executescript` if you want to execute multiple SQL statements with one
849849
call.
850850

@@ -1098,14 +1098,20 @@ Blob Objects
10981098
Exceptions
10991099
----------
11001100

1101+
The exception hierarchy is defined by the DB-API 2.0 (:pep:`249`).
1102+
11011103
.. exception:: Warning
11021104

1103-
A subclass of :exc:`Exception`.
1105+
This exception is not currently raised by the ``sqlite3`` module,
1106+
but may be raised by applications using ``sqlite3``,
1107+
for example if a user-defined function truncates data while inserting.
1108+
``Warning`` is a subclass of :exc:`Exception`.
11041109

11051110
.. exception:: Error
11061111

1107-
The base class of the other exceptions in this module. It is a subclass
1108-
of :exc:`Exception`.
1112+
The base class of the other exceptions in this module.
1113+
Use this to catch all errors with one single :keyword:`except` statement.
1114+
``Error`` is a subclass of :exc:`Exception`.
11091115

11101116
.. attribute:: sqlite_errorcode
11111117

@@ -1121,34 +1127,60 @@ Exceptions
11211127

11221128
.. versionadded:: 3.11
11231129

1130+
.. exception:: InterfaceError
1131+
1132+
Exception raised for misuse of the low-level SQLite C API.
1133+
In other words, if this exception is raised, it probably indicates a bug in the
1134+
``sqlite3`` module.
1135+
``InterfaceError`` is a subclass of :exc:`Error`.
1136+
11241137
.. exception:: DatabaseError
11251138

11261139
Exception raised for errors that are related to the database.
1140+
This serves as the base exception for several types of database errors.
1141+
It is only raised implicitly through the specialised subclasses.
1142+
``DatabaseError`` is a subclass of :exc:`Error`.
1143+
1144+
.. exception:: DataError
1145+
1146+
Exception raised for errors caused by problems with the processed data,
1147+
like numeric values out of range, and strings which are too long.
1148+
``DataError`` is a subclass of :exc:`DatabaseError`.
1149+
1150+
.. exception:: OperationalError
1151+
1152+
Exception raised for errors that are related to the database's operation,
1153+
and not necessarily under the control of the programmer.
1154+
For example, the database path is not found,
1155+
or a transaction could not be processed.
1156+
``OperationalError`` is a subclass of :exc:`DatabaseError`.
11271157

11281158
.. exception:: IntegrityError
11291159

11301160
Exception raised when the relational integrity of the database is affected,
11311161
e.g. a foreign key check fails. It is a subclass of :exc:`DatabaseError`.
11321162

1133-
.. exception:: ProgrammingError
1163+
.. exception:: InternalError
11341164

1135-
Exception raised for programming errors, e.g. table not found or already
1136-
exists, syntax error in the SQL statement, wrong number of parameters
1137-
specified, etc. It is a subclass of :exc:`DatabaseError`.
1165+
Exception raised when SQLite encounters an internal error.
1166+
If this is raised, it may indicate that there is a problem with the runtime
1167+
SQLite library.
1168+
``InternalError`` is a subclass of :exc:`DatabaseError`.
11381169

1139-
.. exception:: OperationalError
1170+
.. exception:: ProgrammingError
11401171

1141-
Exception raised for errors that are related to the database's operation
1142-
and not necessarily under the control of the programmer, e.g. an unexpected
1143-
disconnect occurs, the data source name is not found, a transaction could
1144-
not be processed, etc. It is a subclass of :exc:`DatabaseError`.
1172+
Exception raised for ``sqlite3`` API programming errors,
1173+
for example supplying the wrong number of bindings to a query,
1174+
or trying to operate on a closed :class:`Connection`.
1175+
``ProgrammingError`` is a subclass of :exc:`DatabaseError`.
11451176

11461177
.. exception:: NotSupportedError
11471178

1148-
Exception raised in case a method or database API was used which is not
1149-
supported by the database, e.g. calling the :meth:`~Connection.rollback`
1150-
method on a connection that does not support transaction or has
1151-
transactions turned off. It is a subclass of :exc:`DatabaseError`.
1179+
Exception raised in case a method or database API is not supported by the
1180+
underlying SQLite library. For example, setting *deterministic* to
1181+
:const:`True` in :meth:`~Connection.create_function`, if the underlying SQLite library
1182+
does not support deterministic functions.
1183+
``NotSupportedError`` is a subclass of :exc:`DatabaseError`.
11521184

11531185

11541186
.. _sqlite3-blob-objects:

0 commit comments

Comments
 (0)