LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2003, 08:56 AM   #1
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
python; exception handling


Ok, so I'm really new to python, especially the OO exception handling flow of things. I'm writing a class that will be handling MySQLdb calls, as a wrapper. The MySQLdb part of it makes enough sense, but I'm not quite sure how to go about handling connection errors and the like.

My thoughts are:
* The scripts using the class simply make a call to a method that retrieves data from the MySQL DB.
* The method determines if the DB is connected, and takes appropriate action to connect, if not.
* Connection errors (exceptions) should prolly be passed up through the chain of methods so that the calling script can deal with the exception

I don't know if my logic is good or not, quite frankly, because OO is still quite new to me. Can someone take a look at the example I've given and see if I'm going in the right direction, and if not, offer suggestions for logic improvements?

Code:
#!/usr/bin/env python

import sys

class Base:
    def __init__(self):
        self.sqldatabase = 'project_v11'
        self.sqlname     = 'chuck'
        self.sqlpassword = 'merriweather'
        self.db          = None
    def jobSearch(self, searchFor=''):
        try:
            if self.db is None:
                self.sqlConnect()
            print 'connect ok'
        except Exception, e:
            raise Exception, e
    def sqlConnect(self):
        try:
            import MySQLdb
            self.db = MySQLdb.connect(
              host   = 'localhost',
              user   = self.sqlname,
              passwd = self.sqlpassword,
              db     = self.sqldatabase)
        except Exception, e:
            raise Exception, e
    def sqlDone(self):
        try:
            self.db.close()
        except Exception, e:
            raise Exception, e

if __name__=='__main__':
    try:
        base = Base()
        base.jobSearch(searchFor='test')
        base.sqlDone()
    except Exception, e:
        print e
As of this moment, the database "project_v11" does not exist, so when I run the script, it errors out as:
Code:
/usr/lib/python2.3/ccae> ./Base.py
(1049, "Unknown database 'project_v11'")
And that is expected. However, I'm handling the Exceptions are generic, and I assume that this is bad practice. I'm just not sure what way is "correct".

Thanks!!!!

TLD
 
Old 08-19-2003, 03:37 PM   #2
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Eh, as far as I understand it, handling Exceptions has no real "Right Way(tm)" - some people say that you should translate exceptions received by your module into ones FROM your own module before passing them on to the caller, some people say they should see straight through, some people say other things. This looks okay to me, though you probably want to let the constructor take in dbname/user/pass as args Something like:

Code:
    def __init__(self, db='', name='', passwd=''):
        self.sqldatabase = db
        self.sqlname     = name
        self.sqlpassword = passwd
        self.db          = None
and you'd use:

Code:
        base = Base(db='project_v11', name='chuck', passwd='merriweather')
Probably want to have a host attribute too..

But you aren't asking about those things, so I'll stop now There's nothing inherently wrong with catching and re-throwing exceptions encountered, but just remember that you can always finesse things however you want and that there may be situations where it is appropriate. There's no real general solution or paradigm to follow that I am aware of, however.
 
Old 08-20-2003, 02:01 PM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Original Poster
Rep: Reputation: 33
Strike:

Thanks for the heads on that. AFA the sql names and stuff, that is very temporary. That information will be read from a config file eventually.. I was just putting in some defaults for now to test whether the sql stuff was working. (=
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling vipinsharma Programming 2 09-25-2005 11:05 AM
python exception like structure with a timer shanenin Programming 4 08-20-2005 10:44 AM
Exception Handling C++ Hady Programming 3 07-26-2005 11:07 PM
Exception handling compilation errors ashwinipahuja Programming 6 07-03-2004 06:46 AM
eric3 QT python editor exception... cuboidz Linux - Software 0 02-08-2004 11:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration