上一篇说了函数,这个函数当然有系统的也有自己的,如果函数执行发生意外怎么办?简单,丢个异常出去,让后让异常处理函数处理。当然这个异常,最好是少丢,争取把函数的功能设计简单,语义明确,上层调用函数能根据明确的返回值做各种处理。异常是用来处理没有料想到的错误才用的。
Python提供了2种特色来处理异常:
1 异常处理函数
2 断言
1 异常处理函数
捕获异常:
语法结构:
方式1:
try:
do your operations
except (exception1):
execute exception1 process code
except (exception1,…, exception n):
execute exception1 to n process code
else:
if there is no exception then execute (无任何异常就会执行这里)O(∩_∩)O~
实例代码:
方式2:
try:
do your operations
finally:
process
实例代码:
方式3
try:
You do your operations here;
......................
except ExceptionType, Argument:
You can print value of Argument here...
实例代码:
需要说明的重点是except
Except后面的 exception,可以有以下3个状态
1 没有 捕捉所有的异常走这个处理块
2 有一个 捕捉对应这一个的异常走这个处理块
3 有多个 步骤对应的多个异常走这个处理块
4 还可以带参数 这个参数可以传送给,这个处理块使用
附表 python 捕获异常表(官方手册摘录)
EXCEPTION NAME | DESCRIPTION |
Exception | Base class for all exceptions |
StopIteration | Raised when the next() method of an iterator does not point to any object. |
SystemExit | Raised by the sys.exit() function. |
StandardError | Base class for all built-in exceptions except StopIteration and SystemExit. |
ArithmeticError | Base class for all errors that occur for numeric calculation. |
OverflowError | Raised when a calculation exceeds maximum limit for a numeric type. |
FloatingPointError | Raised when a floating point calculation fails. |
ZeroDivisonError | Raised when division or modulo by zero takes place for all numeric types. |
AssertionError | Raised in case of failure of the Assert statement. |
AttributeError | Raised in case of failure of attribute reference or assignment. |
EOFError | Raised when there is no input from either the raw_input() or input() function and the end of file is reached. |
ImportError | Raised when an import statement fails. |
KeyboardInterrupt | Raised when the user interrupts program execution, usually by pressing Ctrl+c. |
LookupError | Base class for all lookup errors. |
IndexError | Raised when an index is not found in a sequence. |
KeyError | Raised when the specified key is not found in the dictionary. |
NameError | Raised when an identifier is not found in the local or global namespace. |
UnboundLocalError | Raised when trying to access a local variable in a function or method but no value has been assigned to it. |
EnvironmentError | Base class for all exceptions that occur outside the Python environment. |
IOError | Raised when an input/ output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. |
OSError | Raised for operating systemrelated errors. |
SyntaxError | Raised when there is an error in Python syntax. |
IndentationError | Raised when indentation is not specified properly. |
SystemError | Raised when the interpreter finds an internal problem, but when this error is encountered the Python interpreter does not exit. |
SystemExit | Raised when Python interpreter is quit by using the sys.exit() function. If not handled in the code, causes the interpreter to exit. |
TypeError | Raised when an operation or function is attempted that is invalid for the specified data type. |
ValueError | Raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified. |
RuntimeError | Raised when a generated error does not fall into any category. |
NotImplementedError | Raised when an abstract method that needs to be implemented in an inherited class is not actually implemented. |
Finally型的try块,无论是否捕捉到异常都会执行finally块的代码
上面说了异常时意外出错时,系统抛出来的。当然异常也可以由咱们自己制造并抛出来。我们可以通过raise函数来抛出异常。
raise [Exception [, args [, traceback]]]
参数有3个:
1 异常名称
2 给异常处理传递的参数 (可选)
3 这个据官方说没人用(可选)官方网站上说的:rarely used in practice
2断言 用来debug程序的和c语言一样
语法定义:
assert Expression[, Arguments]
参数:
1 表达式 (一点表达式为假就会断了你)
2 打算显示的内容