python学习第十八天 --错误&异常处理
这一章节主要讲解python的错误和异常处理
什么是错误和异常?及其区别?
错误:
try-except用法
try:
try_suite
except Exception[e]:
exception_block
else:
nor - exception
try:
a = int('dd')
except ValueError,e:
print 'catch exception: %s'% e catch exception: invalid literal for int() with base 10: 'dd'
try:
a = int('')
except ValueError,e:
print 'catch exception: %s'% e
else:
print 'nor exception' nor exception
5.一旦try捕获到了错误,接下来错误后的代码将不执行,直接执行except.
try:
a = int('dd')
print 'success convert to int!'//一旦出现错误被捕获,这句代码将不执行
except ValueError,e:
print 'catch exception: %s'% e
else:
print 'nor exception' catch exception: invalid literal for int() with base 10: 'dd'
6.处理多个异常时,如果捕获到异常后,会按顺序逐个匹配。
try:
a = int('dd')
b = 20/0
print 'success convert to int!'
except ZeroDivisionError,e:
print 'catch ZeroDivisionError:%s'% e
except ValueError,e:
print 'catch ValueError: %s'% e
else:
print 'nor exception' catch ValueError: invalid literal for int() with base 10: 'dd'
try-finally用法
try:
try_suite
finally:
do_finally
try:
b = 20/0
finally:
print 'run to finally' run to finally Traceback (most recent call last):
File "<pyshell#178>", line 2, in <module>
b = 20/0
ZeroDivisionError: integer division or modulo by zero
try:
b = 20/2 //不管是否有错误,都会执行finally
finally:
print 'run to finally' run to finally
情况二:
try:
try_suite
except:
do_except
finally:
do_finally
try:
b = 20/0
except ZeroDivisionError,e:
print 'catch ZeroDivisionError %s'% e
finally:
print 'run to finally' catch ZeroDivisionError integer division or modulo by zero
run to finally//即使捕获到异常后,都会执行finally
情况三:
try:
try_suite
except:
do_except
else:
do_else
finally:
do_finally
try:
a = ''
b = 20/(int(a))
except ValueError,e:
print 'catch ValueError:%s'% e
else:
print 'nor exception'
finally:
print 'run to finally' nor exception
run to finally
raise 和 assert用法
while True:
num = int(raw_input('enter 1~100:'))
if num == 0:
raise ZeroDivisionError('Value invalid')//主动抛出异常,中断程序
else:
result = 100/num enter 1~100:0 Traceback (most recent call last):
File "<pyshell#208>", line 4, in <module>
raise ZeroDivisionError('Value invalid')
ZeroDivisionError: Value invalid
def bar(s):
num = int(s)
assert num!=0,'n is zero!'
return 10/num >>> bar('') Traceback (most recent call last):
File "<pyshell#217>", line 1, in <module>
bar('')
File "<pyshell#216>", line 3, in bar
assert num!=0,'n is zero!'
AssertionError: n is zero
启动Python解释器时可以用-O参数来关闭assert。
>>>python -o error.py
关闭后,你可以把所有的assert语句当成pass来看。
python学习第十八天 --错误&异常处理的更多相关文章
- Python学习 Part6:错误和异常
Python学习 Part6:错误和异常 两种不同类型的错误:语法错误和异常 1. 语法错误 语法错误,也被称作解析错误: >>> while True print('Hello w ...
- Python学习笔记七-错误和异常
程序员总是和各种错误打交道,学习如何识别并正确的处理程序错误是很有必要的. 7.1错误和异常 1.错误 从软件方面来看,错误分为语法错误和逻辑错误两种.这两种错误都将导致程序无法正常进行下去,当Pyt ...
- Python学习之路11☞异常处理
一 错误和异常 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 de ...
- python学习笔记014——错误和异常
Python有两种错误很容易辨认:语法错误和异常. 1 什么是语法错误 Python 的语法错误或者称之为解析错,是初学者经常碰到的,如下实例 if i>4 print("if语句输出 ...
- python学习13-类的约束 异常处理 日志
一 .约束 python中约束有两种 第一种,通过抛异常进行约束,这种是子类不按我要求的来,我就给你抛异常(推荐) 操作:提取一个父类. 在父类中给出一个方法.但在方法中不给出任何代码,直接抛异常 # ...
- Python学习日记(二十一) 异常处理
程序中异常的类型 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常规错误的基 ...
- Python学习(17)异常处理
目录 Python 异常处理 Python 标准异常 异常处理 使用except而不带任何异常类型 使用except而带多种异常类型 try-finally 语句 异常参数 异常的参数 用户自定义参数 ...
- Python学习(八)异常处理
Python 异常处理 程序出错时,会抛出异常,这想必在之前学习过程中已经见过不少. 这边具体说明下Python 的标准异常.如何捕捉异常.抛出异常 以及自定义异常. python 标准异常 我们先来 ...
- Python学习笔记整理(十七)异常处理
一.异常基础 try/except:捕捉由代码中的异常并恢复,匹配except里面的错误,并自行except中定义的代码,后继续执行程序(发生异常后,由except捕捉到异常后,不会中断程序,继续执行 ...
随机推荐
- 编写 capture filters
编写 capture filters 如有转载,请在转载前给我提一些建议.谢谢. 百度查不到资料,为无能的百度搜索增加点营养的料. 读 http://www.n-cg.net/CaptureFilte ...
- Web API的CPU占用100%
我用Web API做了一个网站,网站很简单,请求就是几个普通的参数,提交到服务器后,在Web API里做一下参数验证,然后去访问Redis里的TIME命令,最后把TIME命令返回的结果计算出yyyy- ...
- Linux前传——第一次写技术博客
准备写技术博客了.不过,真的没什么技术,以后就写写学习上面遇到的问题与想法吧.
- CPU进程与线程的关系和区别
进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位. ...
- Ubuntu14.04 weblogic11g集群环境测试
在当前域下面新建两个服务器,服务器信息设置: server1:127.0.0.1:7010 server2:127.0.0.1:7020 第一步:新建服务器 (1)进入“服务器”,点击新建: (2)填 ...
- SQL Server 与 Entity Framework 级联删除
SQL Server 级联设置我就不多说了,网上很多教程. 我想提的是 cycles or multiple cascade paths 的问题. 简单的说如果你的级联设置不是一个树型,而是一个带有循 ...
- HDU_2058——等差数列,子集,集合长度判断
Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...
- Codeforces Round #292 (Div. 1) - B. Drazil and Tiles
B. Drazil and Tiles Drazil created a following problem about putting 1 × 2 tiles into an n × m gri ...
- 《SDN核心技术剖析和实战指南》2.2OpenFlow交换机规范小结
这节的内容相当多,主要是介绍OpenFlow标准特别是流表的相关特性. 典型的OpenFlow标准由三部分组成:安全通道,流表和OpenFlow协议.注意这个标准是一份设备标准,并不单指OpenFlo ...
- eclipse svn插件安装
1,在线直接安装 help --> Eclipse Marketplace 2,下载subclipse.zip 把features.plugins拷贝到eclipse安装目录 3,直接把subc ...