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

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)
    
    '''
    result
    except <type 'exceptions.AssertionError'>
    except <type 'exceptions.Exception'>
    '''
  2. 异常信息类型为unicode的中文

    try:
       assert 1 > 2,u'异常'
    except Exception,e:
       pass
    
    try:
       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).

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值