文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>python 异常处理

python 异常处理

时间:2010-06-28  来源:liukaiyi





import sys,traceback

try :

    .....

except :
    exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
    _logger.debug('[error] row %s '% ( ... ))
    # 日志输出
    _logger.error(exceptionType, exceptionValue, exceptionTraceback,limit=10)
    traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,limit=2)




日志类 :

#!/usr/bin/python
#encoding: utf-8
import sys,traceback

class loggingUtils :
    def __init__(self,logfile) :
        import logging
        self.logger = logging.getLogger(logfile)
        self.hdlr = logging.FileHandler(logfile)
        formatter = logging.Formatter('%(asctime)s %(levelname)s - %(message)s')
        self.hdlr.setFormatter(formatter)
        self.logger.addHandler(self.hdlr)
        self.logger.setLevel(logging.DEBUG)

    def debug(self,msg):
        self.logger.debug( msg )
        self.hdlr.flush()
        #logger.removeHandler( hdlr )网络上的实现基本为说明这条语句的使用和作用


    def error(self,exceptionType, exceptionValue, exceptionTraceback,limit=None):
        if limit is None:
            if hasattr(sys, 'tracebacklimit'):
                limit = sys.tracebacklimit
        n = 0
        eline = '\n'
        while exceptionTraceback is not None and (limit is None or n < limit):
            f = exceptionTraceback.tb_frame
            lineno = exceptionTraceback.tb_lineno
            co = f.f_code
            filename = co.co_filename
            name = co.co_name
            eline += ' File "%s", line %d, in %s \n ' % (filename,lineno,name)
            exceptionTraceback = exceptionTraceback.tb_next
            n = n+1

        eline+= "\n".join(traceback.format_exception_only(exceptionType, exceptionValue))
        self.logger.error(eline)
        self.hdlr.flush()




一个 python 连 mysql 的异常处理

#encoding: utf-8
import ConfigParser
import MySQLdb
import time,os,sys,re
import traceback


'''
API 使用 :
from utils import *
db=DB()

for row in db.select('select * from pu_feed_user ;') :
    print row

print db.ddl('insert into pu_feed_user (fuid,uid,foid,valid) values( 2,12,13,14 ) ')

db.close()

'''
class DB():
    def __init__(self):
        config = ConfigParser.RawConfigParser()
        config.read('/........./...conf')
        # 使用 conn = getConn()
        self.conn = MySQLdb.connect(
                        host=config.get('db', 'host'), port=config.getint('db', 'port') ,
                        user=config.get('db', 'user'), passwd=config.get('db', 'passwd'),
                        db=config.get('db', 'database'), charset=config.get('db', 'charset'))

    def select(self,sql):
        cursor = self.conn.cursor ()
        try :
            try :
                cursor.execute (sql)
                return cursor.fetchall ()
            except :
                exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
                traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,limit=2)
        finally :
            try:
                cursor.close()
            except :
                cursor = None

    def ddl(self,sql):
        cursor = self.conn.cursor ()
        try :
            try :
                cursor.execute (sql)
                return cursor.rowcount
            except :
                exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
                traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,limit=2)
        finally :
            try:
                self.conn.commit ()
                cursor.close()
            except :
                cursor = None

    def close(self):
        self.conn.close()


相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载