trapping a MySql warning

Warnings are just that: warnings. They get reported to (usually) stderr, but nothing else is done. You can’t catch them like exceptions because they aren’t being raised.

You can, however, configure what to do with warnings, and turn them off or turn them into exceptions, using the warnings module. For instance, warnings.filterwarnings('error', category=MySQLdb.Warning) to turn MySQLdb.Warning warnings into exceptions (in which case they would be caught using your try/except) or 'ignore' to not show them at all. You can (and probably should) have more fine-grained filters than just the category.

Leave a Comment