This property can be assigned a value of
False to enable or disable whether
warnings should raise exceptions. The default is
False (default). The property can be invoked
to retrieve the current exceptions setting.
raise_on_warnings also sets
get_warnings because warnings need to be
fetched so they can be raised as exceptions.
You might always want to set the SQL mode if you would like to have the MySQL server directly report warnings as errors (see Section 10.2.45, “Property MySQLConnection.sql_mode”). It is also good to use transactional engines so transactions can be rolled back when catching the exception.
Result sets needs to be fetched completely before any exception can be raised. The following example shows the execution of a query that produces a warning:
>>> cnx.raise_on_warnings = True >>> cursor.execute('SELECT "a"+1') >>> cursor.fetchall() .. mysql.connector.errors.DataError: 1292: Truncated incorrect DOUBLE value: 'a'