Python Program is Terminating on Handled Exception
Question: I am editing the question for clarity.

I created a class in a module that imports MySQLdb. I have found that MySQLdb
will raise an Exception if the table the user passes to my class does not
exist. I am catching that exception, and passing a new one to the client. But
even if the client catches my exception, the program still terminates. I have
also tried passing to the client the same Exception MySQLdb is giving my
class.

Since the client will be calling my method in a loop, passing in various table
names, I don't want the program to choke if the table name is bad. I'd like
the client's iterations to continue to the next valid table name. Here is a
snippet (tableName is an arg pass in to the method):

In my class/module:

    
    
        self.tableName = tableName
        try:       
            self.cursor.execute("select * from " + self.tableName + ";") #Will raise Exception if the table does not exist.
        except:
            raise MyException("\n*** " + self.tableName + " does not exist. ***")
    

In the client:

    
    
        tables = ["BadTableName", "GoodTableNameA", "GoodTableNameB","GoodTableNameC"]
    
        try:
            for table in tables:
                my_method(table) #Exception message gets passed in with BadTableName, but the program ends. Subsequest iterations of this loop never happen
        except Exception, e:
            print e
    
    

I would like the client to continue even after calling
my_method(BadTableName).

By the way, my_method() is part of a class defined in its own module which the
client is importing.

Thank you.


Answer: Exceptions do not always cause the program to end. From the
[docs](http://docs.python.org/2/tutorial/errors.html#exceptions):

> Errors detected during execution are called exceptions and are not
> unconditionally fatal:

If an exception is raised in a try: except block and the `except` clause is
specifying the exception or a super class of the exception it will be handled.

When handling an exception you can inspect it and find out all information
related to it. I have never used the
[`traceback`](http://docs.python.org/2/library/traceback.html#traceback-
examples) module before but it looks like it contains everything you need.

I guess your second question depends on what you want your user to see. Should
the exception be shown? (you have access to the traceback). Should just the
message be shown? SHould you log the traceback?

