24 Python assert,Exception的异常信息为unicode的中文时

来源:互联网 发布:网络用语开车的意思 编辑:程序博客网 时间:2024/05/23 21:16

Python assert,Exception的异常信息

  1. 异常信息类型为str

    try:   assert 1 > 2,'except'except Exception,e:   print e,type(e)try:   raise Exception('except')except Exception,e:   print e,type(e)'''resultexcept <type 'exceptions.AssertionError'>except <type 'exceptions.Exception'>'''
  2. 异常信息类型为unicode的中文

    try:   assert 1 > 2,u'异常'except Exception,e:   passtry:   raise Exception(u'异常')except Exception,e:   pass

    没问题…继续看下面

    try:   assert 1 > 2,u'异常'except Exception,e:   print type(e),'e = ',e  #<type 'exceptions.AssertionError'> e =  try:   raise Exception(u'异常')except Exception,e:   print type(e),'e = ',e  #UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

    so使用unicode类型作为异常信息时请小心…


转载请标明出处,原文地址(http://blog.csdn.net/lis_12/article/details/53929366).

如果觉得本文对您有帮助,请点击‘顶’支持一下,您的支持是我写作最大的动力,谢谢。

0 0
原创粉丝点击