python 教程 第十一章、 异常
第十一章、 异常
1) try/except/else格式
try:
s = raw_input('--> ')
except EOFError:
print 'Why did you do an EOF on me?'
except:
print 'Error occurred.'
else:
print 'Done'
except参数说明:
except: Catch all (or all other) exception types.
except name: Catch a specific exception only.
except name as value: Catch the listed exception and its instance.
except (name1, name2): Catch any of the listed exceptions.
except (name1, name2) as value: Catch any listed exception and its instance.
else: Run if no exceptions are raised.
finally: Always perform this block.
2) try/finally格式
try:
fd=open("have-exists-file", "r")
finally:
fd.close()
3) try/except/else/finally通用格式
try:
main-action
except Exception1:
handler1
except Exception2:
handler2
...
else:
else-block
finally:
finally-block
4) assert语句
用来声明某个条件是真的,并且在它非真的时候引发一个错误
assert len('abc') < 1
5) raise引发异常
class ShortInputException(Exception):
def __init__(self, length):
Exception.__init__(self)
self.length = length
try:
s = raw_input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s))
except ShortInputException, x:
print 'Exception: length %d ' % (x.length)
else:
print 'No exception.'
python 教程 第十一章、 异常的更多相关文章
- python 教程 第二十一章、 扩展Python
第二十一章. 扩展Python /* D:\Python27\Lib\Extest-1.0\Extest2.c */ #include <stdio.h> #include <std ...
- 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(二) controller
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第二十一章-授予身份与切换身份(二) 1.回顾 ...
- 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(一) table、entity、service、dao
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第二十一章 授予身份与切换身份(一) 1.使用场景 某个领导因为某 ...
- [core java学习笔记][第十一章异常断言日志调试]
第11章 异常,断言,日志,调试 处理错误 捕获异常 使用异常机制的技巧 使用断言 日志 测试技巧 GUI程序排错技巧 使用调试器 11.1 处理错误 11.1.1异常分类 都继承自Throwable ...
- Flask 教程 第二十一章:用户通知
本文翻译自The Flask Mega-Tutorial Part XXI: User Notifications 这是Flask Mega-Tutorial系列的第二十一章,我将添加一个私有消息功能 ...
- Flask 教程 第十一章:美化
本文翻译自The Flask Mega-Tutorial Part XI: Facelift 这是Flask Mega-Tutorial系列的第十一部分,我将告诉你如何用基于Bootstrap用户界面 ...
- Python开发【十一章】:数据库操作Memcache、Redis
一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...
- 进击的Python【第十一章】:消息队列介绍、RabbitMQ&Redis的重点介绍与简单应用
消息队列介绍.RabbitMQ.Redis 一.什么是消息队列 这个概念我们百度Google能查到一大堆文章,所以我就通俗的讲下消息队列的基本思路. 还记得原来写过Queue的文章,不管是线程queu ...
- python 教程 第十七章、 网络编程
第十七章. 网络编程 1) FTP客户端 import ftplib import os import socket HOST = '127.0.0.1' DIRN = 'menus' FILE ...
随机推荐
- SIP对话、事务详解
1,SIP对话的建立(图片来自于网络) SIP对话的建立包括invite request,response,ACK.其中response包含临时响应(1XX response)和最终响应(非1XX r ...
- 《Java设计模式》之抽象工厂模式
场景问题 举个生活中常见的样例--组装电脑.我们在组装电脑的时候.通常须要选择一系列的配件,比方CPU.硬盘.内存.主板.电源.机箱等. 为讨论使用简单点.仅仅考虑选择CPU和主板的问题. 其实,在选 ...
- 微服务API模拟框架frock介绍
本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2016/02/introducing-frock Urban Airship是一家帮助 ...
- centos7 安装php环境和安装swoole
这仅是我在网上找了多个解决方法,搞定了我遇到的问题,做的一个记录,买这个服务器就是为了测试swoole,结果快到期了,swoole还没装好 感谢https://www.cnblogs.com/phpw ...
- linux系统下安装与配置apache
搭建环境:VMware上虚拟的linux 主机:win 7 安装linux下的Apache前准备: 1.httpd服务的配置文件,默认存储路径:/etc/httpd/conf/httpd.conf( ...
- gcc编译选项--转
gcc提供了大量的警告选项,对代码中可能存在的问题提出警告,通常可以使用-Wall来开启以下警告: -Waddress -Warray-bounds (only with -O2) ...
- 【t070】二进制
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 求所有可以只用1和00拼成的长度为N的二进制数的个数除以15746的余数. 比如当N=4的时候,有5个 ...
- strong & weak 的理解
import "ViewController.h" @interface ViewController () /*weak*/ @property (nonatomic,weak) ...
- Django之文章归档
1.任务描述:将博文按照时间月份归档 2.源代码: views.py def getPage(request, article_list): paginator = Paginator(article ...
- 从多路搜索树到 B-树
1. 什么是 B 树 B 树是为磁盘或其他直接存取的辅助存储设备而设计的一种平衡二叉树: B 树类似于红黑树,但它们在降低磁盘 I/O 操作数方面要更好一点, 许多数据库系统使用 B 树或者 B 树的 ...