Python感觉是模仿Java, 到处都需要加try..catch...。

这里记录一下用法,方便后续使用。

 # -*- coding:utf-8 -*-

 import os
import logging
import traceback #设置log, 这里使用默认log
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='[%Y-%m_%d %H:%M:%S]',
filename=os.path.dirname(os.path.realpath(__file__)) + "/" + 'test.log',
filemode='a') a=10
b=0 #设置为0, 走异常流程; 否则, 走正常流程 try:
res=a/b
logging.info("exec success, res:" + str(res))
except Exception,e:
#format_exc()返回字符串,print_exc()则直接给打印出来
traceback.print_exc()
logging.warning("exec failed, failed msg:" + traceback.format_exc())

logging默认打印级别是warning.

format_exc()返回字符串,print_exc()则直接给打印出来

日志打印:

[2017-11_17 07:51:02] trace.py[line:24] WARNING exec failed, failed msg:Traceback (most recent call last):
File "trace.py", line 19, in <module>
res=a/b
ZeroDivisionError: integer division or modulo by zero

Python traceback 模块, 打印异常信息的更多相关文章

  1. Python logging 模块打印异常 exception

    logger.exception(sys.exc_info())

  2. [python]打印异常信息的不同方式

    异常捕获 try: execpt Exception as e: print(str(e)) 打印异常信息的方式 1.str(e) 返回字符串类型,只给出异常信息,不包括异常信息的类型,如1/0的异常 ...

  3. Python递归_打印节点信息

    Python递归_打印节点信息 递归特性:1.必须由一个明确的结束条件2.每次进入更深一层递归时,问题规模相比上一次递归都应该有所减少3.递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用时 ...

  4. Python traceback 模块,追踪错误

    Python traceback 模块,追踪错误 import traceback try: your code except: traceback.print_exc()

  5. 存储过程——异常捕获&打印异常信息

    目录 0. 背景说明 1. 建立异常信息表ErrorLog 2. 建立保存异常信息的存储过程 3. 建立在SQL Server中打印异常信息的存储过程 4. 建立一个用于测试的存储过程抛出异常进行测试 ...

  6. Python traceback模块简单使用

    Python中的traceback模块被用于跟踪异常返回信息,可以在logging中记录下traceback. traceback.format_exc() 获取异常为字符串,保存到日志文件 try: ...

  7. [Python]获取子线程异常信息

    起因 今天在写东西的时候,用到了多线程.遇到了个问题: 子线程的异常,在父线程中无法捕获. 解决 问题代码 问题代码示例代码如下: import threading class SampleThrea ...

  8. Python基础+模块、异常

    date:2018414+2018415 day1+2 一.python基础 #coding=utf-8 #注释 #算数运算 +(加)  -(减)  *(乘)  /(除)  //(取整)  %(取余) ...

  9. 自动化之路 python psutil模块 收集硬件信息

    一.psutil模块 1. psutil是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系统监控,分析和限制系统资源及进程的管理.它实现了 ...

随机推荐

  1. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. SmartSVN11 Mac版 注册码序列号

    Name=apipostAddress=1337 iNViSiBLE Str.Email=3257132998@qq.comFreeUpdatesUntil=2099-09-26LicenseCoun ...

  3. C#、Java、Javascript获取Unix时间戳

    背景: 因为项目需要,需要几种语言联动开发,日期字段设计的数字型 获取Unix时间戳代码: Java System.currentTimeMillis() Javascript new Date(). ...

  4. Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题

    B. Chris and Magic Square 题目连接: http://www.codeforces.com/contest/711/problem/B Description ZS the C ...

  5. springmvc.xml 上传文件的配置

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.Comm ...

  6. 使用ptrace向已运行进程中注入.so并执行相关函数(转)

    1. 简介 使用ptrace向已运行进程中注入.so并执行相关函数,其中的“注入”二字的真正含义为:此.so被link到已运行进程(以下简称为:目标进程)空间中,从而.so中的函数在目标进程空间中有对 ...

  7. Dynamic-Link Library Redirection

    Dynamic-Link Library Redirection Applications can depend on a specific version of a shared DLL and s ...

  8. Java -verbose[:class|gc|jni] 转 ----JAVA源码分析

    http://blog.csdn.net/tenderhearted/article/details/39642275 http://www.cnblogs.com/iceAeterNa/p/4876 ...

  9. Python yield使用

    https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 您可能听说过,带有 yield 的函数在 Python 中被称 ...

  10. 自己动手写Vue插件Toast

    <style> .vue-toast { width: 100%; height: 100%; position: fixed; top: 0px; left: 0px; backgrou ...