笔记-python异常信息输出

1.      异常信息输出

python异常捕获使用try-except-else-finally语句;

在except 语句中可以使用except as e,然后通过e得到异常信息;

  1. str(e): # 返回字符串类型,只给出异常信息,不包括异常信息的类型,如I/O的异常信息。

division by zero

  1. repr(e): #给出较全的异常信息,包括异常信息的类型

ZeroDivisionError('division by zero',)

  1. e.message #信息最为丰富,一般情况下使用这个选项就足够了
  2. traceback# 获取信息最全

2.      traceback

This module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter.

The module uses traceback objects — this is the object type that is stored in the sys.last_traceback variable and returned as the third item from sys.exc_info().

比较常用的方法有:

print_exception(etype, value, tb [ ,limit [,file ] ])

print_exc([limit [ , file ] ]) # 它是一个print_exception的简写,打印信息

format_exc() # 与print_exc类似,但它返回一个字符串,而不是打印

案例代码:

import traceback
import sys

class Myex_ValueError(Exception):
    """
    my exception
    """
   
def __init__(self, msg):
        self.message
= msg

try:
    a = 5
    b = 5/0
    raise Myex_ValueError('error')
except Exception as e:
    #print(e)
    #print(str(e))
    #print(repr(e))
    #print(e.__class__)
    #print(e.message)
    #traceback.print_exc()
   
print(traceback.format_exc())
    #traceback.print_stack()

3.     
sys

上面提到了traceback会生成traceback 对象,而这个对象可以通过sys.exc_info()得到。

sys.exc_info()的返回值是一个tuple,
(type, value/message, traceback)

这里的type ---- 异常的类型

value/message ---- 异常的信息或者参数

traceback ---- 包含调用栈信息的对象。

笔记-python异常信息输出的更多相关文章

  1. python异常信息获取

    1.python调试的时候获取异常信息 import traceback print '######################################################## ...

  2. springmvc 结合 自动封装异常信息输出为json 报错 500内部服务器错误的原因

    补充:还有一个原因是因为spring的对象没有被成功注入,例如 mapper没有被成功注入,抛出异常时在这种封装场景下将会抛出 500 服务器内部错误, 这种情况下要排查还是靠debug然后看看到底是 ...

  3. StringWriter/PrintWriter在Java输出异常信息中的作用

    闲来无事,看看JUnit的源代码.刚刚开始看就发现一段有趣的代码: public String trace() { StringWriter stringWriter = new StringWrit ...

  4. python学习笔记5_异常

    python学习笔记5_异常 1.什么事异常 Python使用异常对象(exception object) 来表示异常情况.遇到错误会发生异常. 如果异常对象未被处理或被捕捉,程序就会用所谓的回溯(t ...

  5. python使用traceback获取详细的异常信息

    原创来自:https://blog.csdn.net/mengtao0609/article/details/55049059 python使用traceback获取详细的异常信息 2017年02月1 ...

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

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

  7. 一文教你读懂Python中的异常信息

    正文共:11813 字 2 图 预计阅读时间: 30 分钟 原文:https://realpython.com/python-traceback/ 译者:陈祥安 原文有所改动. 在写 Python 代 ...

  8. PHP 错误与异常 笔记与总结(15 )使用观察者模式处理异常信息

    使异常处理变得更灵活.可观察,可以使用设计模式中的观察者模式. 文件 ① 定义观察者的接口 ExceptionObserver.php: <?php /* 给观察者定义的规范 */ interf ...

  9. PHP 错误与异常 笔记与总结(14 )记录和发送异常信息

    当发生异常时,把异常信息记录到日志文件中: <?php header('content-type:text/html; charset=utf-8'); class LogException e ...

随机推荐

  1. django 定制admin

    https://www.cnblogs.com/liwenzhou/p/9519321.html

  2. springboot项目搭建:结构和入门程序

    Spring Boot 推荐目录结构 代码层的结构 根目录:com.springboot 1.工程启动类(ApplicationServer.java)置于com.springboot.build包下 ...

  3. org.springframework.beans.MethodInvocationException: Property 'cacheManager' threw exception; nested exception is org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Caches cannot be

    shiro cache manage配置报错: org.springframework.beans.MethodInvocationException: Property 'cacheManager' ...

  4. ERP和C4C中的function location

    SAP ERP里的Functional Locations,下载到SAP Cloud for Customer后成为类型为'Functional Location'的Installation Poin ...

  5. 爬虫第一篇基本库的使用——urllib

    在Python2中有urllib2和urllib3两个库来实现请求的发送,在Pyhon3中则统一为urllib. urilib包含以下4个模块 request:最基本的请求模块,可以用来实现请求的发送 ...

  6. 二叉搜索树实现MAP

    二叉搜索树的基本实现. /* Date: 2014-04-29 purpose: An implementation of MAP using binary search tree. */ #ifnd ...

  7. hdu-2844&&POJ-1742 Coins---多重背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2844 题目大意: Tony想要买一个东西,他只有n中硬币每种硬币的面值为a[i]每种硬币的数量为c[ ...

  8. bzoj2568 比特集合

    Description 比特集合是一种抽象数据类型(Abstract Data Type) ,其包含一个集合S,并支持如下几种操作: INS M : 将元素 M 插入到集合S中: DEL M : 将集 ...

  9. 数长方形有多少个?POJ(1693)

    题目链接:http://poj.org/problem?id=1693 解题报告: 随机选两根横的,再找一下与这两根横线相交的竖线有多少根,m,那么就有(m-1)*m/2个长方形. #include ...

  10. nbu备份虚拟机报错156状态4207

    VMware Backup getting snapshot error encountered (156)and status: 4207: Could not fetch snapshot met ...