python基础教程_学习笔记10:异常
异常
什么是异常
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:异常的更多相关文章
- python基础教程_学习笔记14:标准库:一些最爱——re
标准库:一些最爱 re re模块包括对正則表達式的支持,由于以前系统学习过正則表達式,所以基础内容略过,直接看python对于正則表達式的支持. 正則表達式的学习,见<Mastering Reg ...
- python基础教程_学习笔记12:充电时刻——模块
充电时刻--模块 python的标准安装包含一组模块,称为标准库. 模块 >>> import math >>> math.sin(0) 0.0 模块是程序 不论什 ...
- python基础教程_学习笔记19:标准库:一些最爱——集合、堆和双端队列
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/36201499 标准库:一些最爱 集合.堆和双端队 ...
- python基础教程_学习笔记1:序列-1
序列 数据结构:通过某种方式组织在一起的数据元素的集合,这些数据元素能够是数字或者字符,甚至能够是其它数据结构. python中,最主要的数据结构是序列. 序列中的每一个元素被分配一个序号--即元素的 ...
- python基础教程_学习笔记9:抽象
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/30745465 抽象 懒惰即美德. 抽象和结构 抽 ...
- python基础教程_学习笔记11:魔法方法、属性和迭代器
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/31417309 魔法方法.属性和迭代器 在pyth ...
- python基础教程_学习笔记18:标准库:一些最爱——shelve
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/36029981 标准库:一些最爱 shelve S ...
- python基础课程_学习笔记13:标准库:有些收藏夹——sys
标准库:有些收藏夹 sys sys这个模块可以让你访问和python解释器联系紧密的变量和函数. sys模块中一些重要的函数和变量 函数/变量 描写叙述 argv 命令行參数,包含脚本名称 exit( ...
- python基础课程_学习笔记15:标准库:有些收藏夹——fileinput
标准库:有些收藏夹 fileinput 重要功能 性能 叙述性说明 input([files[,inplace[,backup]]) 便于遍历多个输入流中的行 filename() 返回当前文件的名称 ...
随机推荐
- asp.net导出Excel类库
using System; using System.Collections.Generic; using System.Reflection; using System.Web; using Exc ...
- 反应堆Reactor
mvn -h 可以看到很多命令及其用途:-am --also-make 同时构建所列模块的依赖模块:-amd -also-make-dependents 同时构建依赖于所列模块的模块:-pl --pr ...
- 用Eclipse做Android开发时出现java.lang.NoClassDefFoundError问题
之前有遇到过这个问题,后来解决了,今天又遇到了,但是忘了当时是怎么解决的,费了好长时间,终于又找回解决的方法,现在记录下来,以防以后又遇到. 这个错误出现在我的某一个Activity,但是我反复确认了 ...
- Codeforces Round #254 (Div. 2)D(预计)
D. DZY Loves FFT time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- MySQL里求给定的时间是所在月份的第几个礼拜
Share 一个昨天写的函数. 目的是求给定的时间是所在月份的第几个礼拜. DELIMITER $$ USE `t_girl`$$ DROP FUNCTION IF EXISTS `weekofmon ...
- TODO管理工具TaskWarrior (跨平台C++代码)
Taskwarrior 是一个基于命令行的 TODO 列表管理工具.主要功能包括:标签.彩色表格输出.报表和图形.大量的命令.底层API.多用户文件锁等功能. http://www.oschina.n ...
- Eclipse和PyDev搭建完美Python开发环境(Windows篇)(转)
摘要:本文讲解了用Eclipse和PyDev搭建Python的开发环境. 十一长假在家闲着没事儿,准备花点时间学习一下Python. 今儿花了一个下午搭建Python的开发环境,不禁感叹————开 ...
- JavaScript 进阶(二)变量作用域
局部变量陷阱 先看一段代码: function foo() { var a = "hello" b = "world" return a + b; } 这个函数 ...
- Android 仿 窗帘效果 和 登录界面拖动效果 (Scroller类的应用) 附 2个DEMO及源码
在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现.下面要说的就是上次Scroller ...
- iframe - 基本用法
· 用target的值,指向iframe框架的name值. <body> <form id="form1" runat="server"> ...