异常

什么是异常

Python用异常对象来表示异常情况。遇到错误后,会引发异常。假设异常对象并未被处理或捕捉,程序就会用所谓的回溯(Traceback,一种错误信息)终止运行:

>>> 1/0

Traceback (most recent call last):

File "<pyshell#0>", line 1, in <module>

1/0

ZeroDivisionError: integer division or modulo by zero

每一个异常都是一些类的实例,这些实例能够被引发,而且能够用非常多方法进行捕捉,使得程序能够捉住错误并对其进行处理,而不是让整个程序失败。

按自己的方式出错

异常能够在某些东西出错时自己主动引发。

raise语句

为了引发异常,能够使用一个类(应该是Exception的子类)或者实例參数调用raise语句。使用类时,程序会自己主动创建实例。

>>> raise Exception

Traceback (most recent call last):

File "<pyshell#1>", line 1, in <module>

raise Exception

Exception

>>> raise Exception('hyperdrive overload')

Traceback (most recent call last):

File "<pyshell#2>", line 1, in <module>

raise Exception('hyperdrive overload')

Exception: hyperdrive overload

第一个样例引发了一个没有不论什么有关错误信息的普通异常;

第二个样例则加入了一些hyperdive overload错误信息。

内建的异常类有非常多。

>>> import exceptions

>>> dir(exceptions)

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__doc__', '__name__', '__package__']

全部这些异常都能够用于raise语句:

>>> raise ZeroDivisionError

Traceback (most recent call last):

File "<pyshell#6>", line 1, in <module>

raise ZeroDivisionError

ZeroDivisionError

>>> raise BaseException

Traceback (most recent call last):

File "<pyshell#7>", line 1, in <module>

raise BaseException

BaseException

一些最重要的内建异常类:

类名 描写叙述
Exception 全部异常的基类
AttributeError 特性引用或赋值失败时引发
IOError 试图打开不存在的文件(包含其它情况)时引发
IndexError 在使用序列不存在的索引时引发
KeyError 在使用映射中不存在的键时引发
NameError 在找不到名字(变量)时引发
SyntaxError 在代码为错误形式时引发
TypeError 在内建操作或函数应用于错误类型的对象时引发
ValueError 在内建操作或者函数应用于正确类型的对象,但该对象使用不合适的值时引发
ZeroDivisionError 在除法或模除操作的第二个參数为0时引发

自己定义异常类

有些时候须要创建自己的异常类。

class SomeCustomException(Exception): pass

能够向自己主动义异常类中添加方法,此处什么也没做。

捕捉异常

关于异常最有意思的地方就是能够处理它们(通常叫做诱捕或者捕捉异常)。

这个功能能够使用try/except来实现。

x=input(‘1st number: ’)

y=input(‘2nd number: ’)

print x/y

假设用户输入0作为第二个数,则出现ZeroDivisionError异常。

为了捕捉异常而且做出一些错误处理:

try:

x=input(‘1st number: ’)

y=input(‘2nd number: ’)

print x/y

except ZeroDivisionError:

print “The 2nd number can’t be zero!”

看,没參数

>>> class MuffledCalculator:

muffled=False

def calc(self,expr):

try:

return eval(expr)

except ZeroDivisionError:

if self.muffled:

print "Division by zero is illegal"

else:

raise

>>> calculator=MuffledCalculator()

>>> calculator.calc('10/2')

5

>>> calculator.calc('10/0')

Traceback (most recent call last):

File "<pyshell#22>", line 1, in <module>

calculator.calc('10/0')

File "<pyshell#19>", line 5, in calc

return eval(expr)

File "<string>", line 1, in <module>

ZeroDivisionError: integer division or modulo by zero

>>> calculator.muffled=True

>>> calculator.calc('10/0')

Division by zero is illegal

不止一个except子句

try:

x=input(‘1st number: ’)

y=input(‘2nd number: ’)

print x/y

except ZeroDivisionError:

print “The 2nd number can’t be zero!”

假设用户输入“HelloWorld”作为第二个參数,将引发错误:

TypeError: unsupported operand type(s) for /: 'int' and 'str'

由于except子句仅仅寻找ZeroDivisionError异常,这次的错误就溜过了检查并导致程序终止。为了捕捉这个异常,能够直接在同一个try/except语句后面加上还有一个except子句:

try:

x=input(‘1st number: ’)

y=input(‘2nd number: ’)

print x/y

except ZeroDivisionError:

print “The 2nd number can’t be zero!”

except TypeError:

print “That wasn’t a number,was it?”

用一个块捕捉两个异常

假设须要用一个块捕捉多个类型异常,能够将它们作为元组列出:

try:

x=input(‘1st number: ’)

y=input(‘2nd number: ’)

print x/y

except (ZeroDivisionError,TypeError):

print “Your numers are bogus!”

捕捉对象

假设想让程序继续执行,可是又由于某种原因想记录下错误,捕捉对象就非常实用。

try:

x=input(‘1st number: ’)

y=input(‘2nd number: ’)

print x/y

except (ZeroDivisionError,TypeError),e:

print e

真正的全捕捉

就算程序能处理好几种类型的异常,但有些异常还是会从眼皮底下溜走。

以除法为样例,在提示符下直接按回车,不输入不论什么东西,会得倒一个类似以下的错误信息:

SyntaxError: unexpected EOF while parsing

这个异常逃过了try/except语句的检查。这样的情况下,与其用那些并不是捕捉这些异常的try/except语句隐藏异常,还不如让程序立马崩溃。

但假设真的想用一段代码捕获全部异常,能够在except子句中忽略全部的异常类:

try:

x=input(‘1st number: ’)

y=input(‘2nd number: ’)

print x/y

except:

print ‘Something wrong happened...’

警告:像这样捕捉全部异常是危急的,由于它会隐藏全部程序猿未想到而且未做好准备处理的错误。它相同会捕捉用户终止运行的Ctrl+c企图,以及用sys.exit函数终止程序的企图,等等。这时用except Exception,e会更好些,或者对异常对象e进行一些检查。

万事大吉

有些情况下,一些坏事发生时运行一段代码是非常实用的,能够给try/except语句加个else子句:

>>> try:

print 'A simple task'

except:

print 'what?'

else:

print 'nothing'

A simple task

nothing

最后......

finally子句,用来在可能的异常后进行清理。

>>> try:

print 'A simple task'

except:

print 'what?'

else:

print 'nothing'

finally:

print 'clean up'

A simple task

nothing

clean up

异常和函数

异常和函数非常自然地一起工作。假设异常在函数内引发而不被处理,它会传播至函数调用的地方。假设在那里也没有处理异常,它会继续传播,一直到达主程序(全局作用域)。假设那里没有异常处理程序,程序会带着堆栈跟踪终止。

>>> def faulty():

raise Exception('something is wrong')

>>> def ignore_exception():

faulty()

>>> def handle_exception():

try:

faulty()

except:

print 'Exception handled'

>>> ignore_exception()

Traceback (most recent call last):

File "<pyshell#58>", line 1, in <module>

ignore_exception()

File "<pyshell#51>", line 2, in ignore_exception

faulty()

File "<pyshell#48>", line 2, in faulty

raise Exception('something is wrong')

Exception: something is wrong

>>> handle_exception()

Exception handled

python基础教程_学习笔记10:异常的更多相关文章

  1. python基础教程_学习笔记14:标准库:一些最爱——re

    标准库:一些最爱 re re模块包括对正則表達式的支持,由于以前系统学习过正則表達式,所以基础内容略过,直接看python对于正則表達式的支持. 正則表達式的学习,见<Mastering Reg ...

  2. python基础教程_学习笔记12:充电时刻——模块

    充电时刻--模块 python的标准安装包含一组模块,称为标准库. 模块 >>> import math >>> math.sin(0) 0.0 模块是程序 不论什 ...

  3. python基础教程_学习笔记19:标准库:一些最爱——集合、堆和双端队列

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/36201499 标准库:一些最爱 集合.堆和双端队 ...

  4. python基础教程_学习笔记1:序列-1

    序列 数据结构:通过某种方式组织在一起的数据元素的集合,这些数据元素能够是数字或者字符,甚至能够是其它数据结构. python中,最主要的数据结构是序列. 序列中的每一个元素被分配一个序号--即元素的 ...

  5. python基础教程_学习笔记9:抽象

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/30745465 抽象 懒惰即美德. 抽象和结构 抽 ...

  6. python基础教程_学习笔记11:魔法方法、属性和迭代器

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/31417309 魔法方法.属性和迭代器 在pyth ...

  7. python基础教程_学习笔记18:标准库:一些最爱——shelve

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/36029981 标准库:一些最爱 shelve S ...

  8. python基础课程_学习笔记13:标准库:有些收藏夹——sys

    标准库:有些收藏夹 sys sys这个模块可以让你访问和python解释器联系紧密的变量和函数. sys模块中一些重要的函数和变量 函数/变量 描写叙述 argv 命令行參数,包含脚本名称 exit( ...

  9. python基础课程_学习笔记15:标准库:有些收藏夹——fileinput

    标准库:有些收藏夹 fileinput 重要功能 性能 叙述性说明 input([files[,inplace[,backup]]) 便于遍历多个输入流中的行 filename() 返回当前文件的名称 ...

随机推荐

  1. 关于如何实现程序一天只启动一次的想法(C++实现)

    问题描述: 我们在程序开发当中,经常会遇到某些子程序需要实现一天只启动一次的功能,该功能实现的方法有很多种,其原理都是通过记录标记为来实现的.本次要分享的也是利用程序标记为来实现的,而且只需要使用一个 ...

  2. javascript笔记整理(对象的继承顺序、分类)

    Object.prototype.say=function(){ alert("我是顶层的方法"); } children.prototype=new parent(); pare ...

  3. 基于visual Studio2013解决面试题之1404希尔排序

     题目

  4. USACO inflate

    全然背包,转化为0/1背包 dp[i, j] = max(dp[i-1, j], dp[i, j - minutes[i]] + points[i]) /* ID:kevin_s1 PROG:infl ...

  5. java对象占用内存大小计算方式

    案例一: User public class User { } UserSizeTest public class UserSizeTest { static final Runtime runTim ...

  6. [置顶] 让导入的Android项目,运行起来的方法。

    Eclipse里面直接import的代码,不能运行出现如下错误: [2013-12-12 12:58:55 - Dex Loader] Unable to execute dex: java.nio. ...

  7. QtWebkit中如何将网页内容转为图片

    原地址:http://www.cnblogs.com/baizx/archive/2010/07/31/1789573.html 如何将webkit中的渲染结果也就是网页画面转换为图片   用抓图软件 ...

  8. 浅谈数据库技术,磁盘冗余阵列,IP分配,ECC内存,ADO,DAO,JDBC

    整理-----数据库技术,磁盘冗余阵列,IP分配, ECC内存,ADO, DAO,JDBC 1.MySQL MySQL是最受欢迎的开源SQL数据库管理系统,它由 MySQL AB开发.发布和支持.My ...

  9. Oracle判断指定列是否全部为数字

    select nvl2(translate(name,'\1234567890 ', '\'),'is   characters ','is   number ') from customer_inf ...

  10. RobotFramework 自定义Library

    RobotFramework 主要使用Python,这里简单自定义Library,以扩充RobotFramework的功能 新建一个python类,自定义需要的方法 例如: 保存成TestLibrar ...