日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python捕获全局异常统一管理_python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中...

發(fā)布時(shí)間:2023/11/30 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python捕获全局异常统一管理_python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用sys.excepthook函數(shù)進(jìn)行全局異常的獲取。

1. 使用MessageDialog實(shí)現(xiàn)異常顯示;

2. 使用logger把捕獲的異常信息輸出到日志中;

步驟:定義異常處理函數(shù), 并使用該函來(lái)替換掉系統(tǒng)的內(nèi)置處理函數(shù);

對(duì)于threading.py的異常捕獲,需要對(duì)該文件進(jìn)行一些改變:

如下:

try:

self.run()

except SystemExit:

if __debug__:

self._note("%s.__bootstrap(): raised SystemExit", self)

except:

if __debug__:

self._note("%s.__bootstrap(): unhandled exception", self)

# If sys.stderr is no more (most likely from interpreter

# shutdown) use self.__stderr. Otherwise still use sys (as in

# _sys) in case sys.stderr was redefined since the creation of

# self.

if _sys:

if id(_sys.excepthook) != id(_sys.__excepthook__):

exc_type, exc_value, exc_tb = self.__exc_info()

_sys.excepthook(exc_type, exc_value, exc_tb)

_sys.stderr.write("Exception in thread %s:\n%s\n" %

(self.name, _format_exc()))

#-*- coding: UTF-8 -*-

#-------------------------------------------------------------------------------

# Name: 模塊except hook handler

# Purpose: 全局捕獲異常

#

# Author: ankier

#

# Created: 17-08-2013

# Copyright: (c) ankier 2013

# Licence:

#-------------------------------------------------------------------------------

import logging

import sys

import traceback

import datetime

import wx

## @detail 創(chuàng)建記錄異常的信息

class ExceptHookHandler(object):

## @detail 構(gòu)造函數(shù)

# @param logFile: log的輸入地址

# @param mainFrame: 是否需要在主窗口中彈出提醒

def __init__(self, logFile, mainFrame = None):

self.__LogFile = logFile

self.__MainFrame = mainFrame

self.__Logger = self.__BuildLogger()

#重定向異常捕獲

sys.excepthook = self.__HandleException

## @detail 創(chuàng)建logger類

def __BuildLogger(self):

logger = logging.getLogger()

logger.setLevel(logging.DEBUG)

logger.addHandler(logging.FileHandler(self.__LogFile))

return logger

## @detail 捕獲及輸出異常類

# @param excType: 異常類型

# @param excValue: 異常對(duì)象

# @param tb: 異常的trace back

def __HandleException(self, excType, excValue, tb):

# first logger

try:

currentTime = datetime.datetime.now()

self.__Logger.info('Timestamp: %s'%(currentTime.strftime("%Y-%m-%d %H:%M:%S")))

self.__Logger.error("Uncaught exception:", exc_info=(excType, excValue, tb))

self.__Logger.info('\n')

except:

pass

# then call the default handler

sys.__excepthook__(excType, excValue, tb)

err_msg = ''.join(traceback.format_exception(excType, excValue, tb))

err_msg += '\n Your App happen an exception, please contact administration.'

# Here collecting traceback and some log files to be sent for debugging.

# But also possible to handle the error and continue working.

dlg = wx.MessageDialog(None, err_msg, 'Administration', wx.OK | wx.ICON_ERROR)

dlg.ShowModal()

dlg.Destroy()

輸出效果:

log輸出文件:

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的python捕获全局异常统一管理_python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。