Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling imported module Exceptions

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 {}:
    ...
like image 289
Cagrosso Avatar asked Oct 23 '16 02:10

Cagrosso


2 Answers

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

like image 52
xtian Avatar answered Sep 20 '22 15:09

xtian


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.

like image 31
Terry Jan Reedy Avatar answered Sep 21 '22 15:09

Terry Jan Reedy