在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!!

有时候我们写程序的时候,会出现一些错误或异常,导致程序终止.

为了处理异常,我们使用try...except

把可能发生错误的语句放在try模块里,用except来处理异常。

except可以处理一个专门的异常,也可以处理一组圆括号中的异常,

如果except后没有指定异常,则默认处理所有的异常。

每一个try,都必须至少有一个except

在python的异常中,有一个万能异常:Exception,他可以捕获任意异常

python 多个 json python 多个try_python 多个 json

python 多个 json python 多个try_代码块_02

s1 = 'hello'
try:
    int(s1)
except Exception,e:
    print e

任意异常Exception

程序时需要考虑到try代码块中可能出现的多个异常,可以这样写:

python 多个 json python 多个try_python 多个 json

python 多个 json python 多个try_代码块_02

s1 = 'hello'
try:
    int(s1)
except IndexError,e:
    print e
except KeyError,e:
    print e
except ValueError,e:
    print e

多个exception

 异常的简单结构和复杂结构

python 多个 json python 多个try_python 多个 json

python 多个 json python 多个try_代码块_02

try:
  pass
except Exception as e:  #python2 中还可以这样写:except Exception,e
  pass

简单实列

python 多个 json python 多个try_python 多个 json

python 多个 json python 多个try_代码块_02

1 try:
 2     # 主代码块
 3     pass
 4 except KeyError,e:
 5     # 异常时,执行该块
 6     pass
 7 else:
 8     # 主代码块执行完,执行该块
 9     pass
10 finally:
11     # 无论异常与否,最终执行该块
12     pass

完整实列

先定义特殊提醒的异常,最后定义Exception,来确保程序正常运行。

python 多个 json python 多个try_python 多个 json

python 多个 json python 多个try_代码块_02

s1 = 'hello'
try:
    int(s1)
except KeyError,e:
    print '键错误'
except IndexError,e:
    print '索引错误'
except Exception, e:
    print '错误'

先特殊,后万能

主动触发异常

raise Exception('messages') 可以自定义报错信息

python 多个 json python 多个try_python 多个 json

python 多个 json python 多个try_代码块_02

a=2
if a > 1:
    raise ValueError('值大于1')

raise

 

python 多个 json python 多个try_python 多个 json

python 多个 json python 多个try_代码块_02

try:
    raise Exception('错误了。。。')
except Exception,e:
    print e

raise 触发异常

 自定义异常

python 多个 json python 多个try_python 多个 json

python 多个 json python 多个try_代码块_02

class WupeiqiException(Exception):
 
    def __init__(self, msg):
        self.message = msg
 
    def __str__(self):
        return self.message
 
try:
    raise WupeiqiException('我的异常')
except WupeiqiException,e:
    print e

自定义异常类

 python所有的标准异常类:

异常名称

描述

BaseException

所有异常的基类

SystemExit

解释器请求退出

KeyboardInterrupt

用户中断执行(通常是输入^C)

Exception

常规错误的基类

StopIteration

迭代器没有更多的值

GeneratorExit

生成器(generator)发生异常来通知退出

SystemExit

Python 解释器请求退出

StandardError

所有的内建标准异常的基类

ArithmeticError

所有数值计算错误的基类

FloatingPointError

浮点计算错误

OverflowError

数值运算超出最大限制

ZeroDivisionError

除(或取模)零 (所有数据类型)

AssertionError

断言语句失败

AttributeError

对象没有这个属性

EOFError

没有内建输入,到达EOF 标记

EnvironmentError

操作系统错误的基类

IOError

输入/输出操作失败

OSError

操作系统错误

WindowsError

系统调用失败

ImportError

导入模块/对象失败

KeyboardInterrupt

用户中断执行(通常是输入^C)

LookupError

无效数据查询的基类

IndexError

序列中没有没有此索引(index)

KeyError

映射中没有这个键

MemoryError

内存溢出错误(对于Python 解释器不是致命的)

NameError

未声明/初始化对象 (没有属性)

UnboundLocalError

访问未初始化的本地变量

ReferenceError

弱引用(Weak reference)试图访问已经垃圾回收了的对象

RuntimeError

一般的运行时错误

NotImplementedError

尚未实现的方法

SyntaxError

Python 语法错误

IndentationError

缩进错误

TabError

Tab 和空格混用

SystemError

一般的解释器系统错误

TypeError

对类型无效的操作

ValueError

传入无效的参数

UnicodeError

Unicode 相关的错误

UnicodeDecodeError

Unicode 解码时的错误

UnicodeEncodeError

Unicode 编码时错误

UnicodeTranslateError

Unicode 转换时错误

Warning

警告的基类

DeprecationWarning

关于被弃用的特征的警告

FutureWarning

关于构造将来语义会有改变的警告

OverflowWarning

旧的关于自动提升为长整型(long)的警告

PendingDeprecationWarning

关于特性将会被废弃的警告

RuntimeWarning

可疑的运行时行为(runtime behavior)的警告

SyntaxWarning

可疑的语法的警告

UserWarning

用户代码生成的警告