Handling Errors

Updated on Jan 07, 2020


Interacting with a database is an error prone process, so we must always implement some mechanism to handle errors gracefully.

MySQLdb has MySQLdb.Error exception, a top level exception that can be used to catch all database exception raised by MySQLdb module.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from __future__ import print_function

import MySQLdb as my

try:

    db = my.connect(host="127.0.0.1",
                    user="root",
                    passwd="",
                    db="world"
                  )

    cursor = db.cursor()

    sql = "select * from city"
    number_of_rows = cursor.execute(sql)
    print(number_of_rows)
    db.close()

except my.Error as e:
    print(e)

except :
    print("Unknown error occurred")

Two main error in MySQLdb #


There are two categories of exception class in MySQLdb you need to be aware:

  1. DatabaseError.
  2. InterfaceError.

  1. DatabaseError: This exception raises when there is problem in data processing, error in sql syntax, mysql internal problems. If a connection is made and a problem arises then DatabaseError will catch it.

  2. InterfaceError: When database connection fails for some reason, MySQLdb will raise an InterfaceError. Note InterfaceError only get raise when there is internal problem in connection to the database, MySQLdb will not raise InterfaceError because of wrong database name or password.

DatabaseError is further divided into 6 types:

  1. DataError.
  2. InternalError.
  3. IntegrityError.
  4. OperationalError.
  5. NotSupportedError.
  6. ProgrammingError.

  1. DataError: MySQLdb throws this error when there is problem in the data processing, like division by zero, numeric value of of range.

  2. InternalError: This exception is raised when there is some internal error in MySQL database itself. For e.g invalid cursor, transaction out of sync etc.

  3. IntegrityError: This exception is raised when foreign key check fails.

  4. OperationalError: This exception is raised for things that are not in control of the programmer. For e.g unexpected disconnect, error in memory allocation etc, selected database not exists.

  5. NotSupportedError: This exception is raised when there is method or api that is not supported.

  6. ProgrammingError: This exception is raised of programming errors. For e.g table not found, error in mysql syntax, wrong number of parameters specified etc.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import print_function

import MySQLdb as my

try:

    db = my.connect(host="127.0.0.1",
                    user="root",
                    passwd="",
                    db="world"
                   )

    cursor = db.cursor()

    sql = "select * from city"
    number_of_rows = cursor.execute(sql)
    print(number_of_rows)
    db.close()

except my.DataError as e:
    print("DataError")
    print(e)

except my.InternalError as e:
    print("InternalError")
    print(e)

except my.IntegrityError as e:
    print("IntegrityError")
    print(e)

except my.OperationalError as e:
    print("OperationalError")
    print(e)

except my.NotSupportedError as e:
    print("NotSupportedError")
    print(e)

except my.ProgrammingError as e:
    print("ProgrammingError")
    print(e)

except :
    print("Unknown error occurred")

In the next post we discuss how to fetch specific number of rows from the database.


Other Tutorials (Sponsors)