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捕捉到异常后,不会中断程序,继续执行 ...
随机推荐
- IOS编程教程(八):在你的应用程序添加启动画面
IOS编程教程(八):在你的应用程序添加启动画面 虽然你可能认为你需要编写闪屏的代码,苹果已经可以非常轻松地把它做在Xcode中.不需要任何编码.你只需要做的是设置一些配置. 什么是闪屏 对于那些 ...
- css的!important规则对性能有影响吗
最近在做项目中发现很多CSS代码里面都使用!important去覆盖原有高优先级的样式.按照常理来说,越是灵活的东西,需要做的工作就会更多.所以想当然的认为像!important这样灵活.方便的规则如 ...
- 转:内核空间与用户空间数据交换的方式之一 --ioctl(通过字符设备演示)
对于linux而言,内核程序和用户程序分别运行在内核空间和用户空间,要实现两者的数据交换,主要有以下几种方式:系统调用,读写系统文件(procfs,sysfs, seq_file,debugfs等), ...
- (摘)Zebra打印机异常处理
一.一般条码打印设备按图指示方向,虚线为碳带安装路径,实线为标签路径.回卷后废碳带不易剥落,则在装入前用废标签的光滑底纸卷在回卷轴上,然后再上碳带.安装标签时,根据不同标签宽度调整限纸器.压头弹簧均匀 ...
- ural 1207 Median on the Plane
极角排序 #include <cstdio> #include <cstring> #include <algorithm> #include <cmath& ...
- Bundle类
1.新建一个Bundle类 Bundle bundle=new Bundle();2.Bundle类中放入数据(key-value的形式,另一个Activity里面取数据的时候,通过key值找出对应的 ...
- Codeforces 235E Number Challenge
http://codeforces.com/contest/235/problem/E 远距离orz......rng_58 证明可以见这里(可能要FQ才能看到) 还是copy一下证明吧: 记 $$f ...
- EF 5.0 帮助类 增删改查
原文地址:http://www.cnblogs.com/luomingui/p/3362813.html EF 5.0 帮助类 加入命名空间: using System; using System.D ...
- SQL-Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- 关于bootstrap--导航栏
1.普通导航:class .nav-tabs. <ul class="nav nav-tabs"> <li class="active"> ...