9

How do you handle module specific exceptions in Python?

For example, if I wanted to catch an error thrown from the sqlite3 module in python, what would I place in the {} to handle that exception?

import sqlite3

try:
    ...
except {}:
    ...
Cagrosso
  • 195
  • 1
  • 12
  • You cannot handle it on the level you suggest. Consider reading [Errors and Exceptions](https://docs.python.org/2.7/tutorial/errors.html) part of python docs at least – agg3l Oct 23 '16 at 02:27
  • @agg3l So you are saying that any error from an action performed by the sqlite3 module cannot be handled? – Cagrosso Oct 23 '16 at 02:32
  • No. I meant that it needs (must be) fine-grained control. On the top level you want to handle it, your only option is to catch all base sqlite3 exceptions (if specific base classs exist) or all exceptions, both related and not related to sqlite3. With sequence show in your code, you are catching everything, starting from 'insert error' to OS access denied.. – agg3l Oct 23 '16 at 02:36
  • @agg3l I have made the question clearer as to what I am looking for. I understand that as I wrote it I was catching all errors thrown by the code in the try block before the edit. – Cagrosso Oct 23 '16 at 02:40
  • It's will simply render syntax error in current form.. Go into reviewing sqlite3 module, looking for base exception class there. This is your only remedy – agg3l Oct 23 '16 at 02:42
  • And, take some time over a while to read though basic Python syntax and program structure ;) – agg3l Oct 23 '16 at 02:43
  • @agg3l Is it as simple as placing `sqlite3.SQLITE3_ERROR_HERE` in the {}? – Cagrosso Oct 23 '16 at 02:46
  • Yes. See my answer for specifics. – Terry Jan Reedy Oct 23 '16 at 03:48

3 Answers3

10

The answer is already here, How to reference an exception class in Python?, though you wouldn't know it by the title.

I read the question (in the current form) as a bog-simple imported class syntax question. The SQLLite docs page on exceptions gives an example, but doesn't describe how to implement the SQLLite exceptions explicitly. And it shouldn't. I believe an example should be given on the Python.org exceptions page, but you won't find even an example there--just this:

"Many standard modules define their own exceptions to report errors that may occur in functions they define. More information on classes is presented in chapter Classes."

You follow the link and now you're on page one, paragraph one of Python Classes. *Smack* If you're a novice, you start reading and if you haven't returned to google, then you get to this,

By the way [my emphasis], I use the word attribute for any name following a dot — for example, in the expression z.real, real is an attribute of the object z. Strictly speaking, references to names in modules are attribute references: in the expression modname.funcname, modname is a module object and funcname is an attribute of it. In this case there happens to be a straightforward mapping between the module’s attributes and the global names defined in the module: they share the same namespace!

Now you're sorted, right? I dunno. I'm familiar with the term 'dot notation' (and less common, 'dot syntax'), but it's not used here. Makes for slow getting around when all of the street signs are down. It's a lot to have to slog through when the exceptions page could give you this simple example,

import <module>

try:
    ...
except <module>.<exception>:
    ...

One more thought on the confusion.

It's common to import specific functions or classes from modules using this syntax,

from [module] import [function/class]

A novice might finding it tough to see the simple answer. I think this common use syntax creates a gap in the mental-model making it hard to connect module import syntax to module use syntax. Here's a great answer on this topic which is a much better citation for Module Exception Importing than the Classes link above

xtian
  • 2,765
  • 7
  • 38
  • 65
1

Proper module docs list the module specific exceptions that the module might raise, so module users can understand and possibly catch them. The sqlite3 docs contain this.

12.6.5. Exceptions

exception sqlite3.Warning
    A subclass of Exception.

exception sqlite3.Error
    The base class of the other exceptions in this module.
    It is a subclass of Exception.

exception sqlite3.DatabaseError
    Exception raised for errors that are related to the database.

exception sqlite3.IntegrityError
    Exception raised when the relational integrity of the database
    is affected, e.g. a foreign key check fails. It is a subclass
    of DatabaseError.

exception sqlite3.ProgrammingError
    Exception raised for programming errors, e.g. table not found
    or already exists, syntax error in the SQL statement, wrong
    number of parameters specified, etc. It is a subclass of
    DatabaseError.

You can catch any of these. The socket module doc, for instance, has a similar section.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
0

Checking through the documentation on the sqlite3 module, the following should work

try:
    #code that throws sqlite3.Error exception
catch sqlite3.Error:
    #code to handle error

That is just one of several errors that this specific module can throw, and each of the errors can be handled by replacing the above sqlite3.Error part with the needed exception.

Cagrosso
  • 195
  • 1
  • 12