第十一章、 异常
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 教程 第十一章、 异常的更多相关文章

  1. python 教程 第二十一章、 扩展Python

    第二十一章. 扩展Python /* D:\Python27\Lib\Extest-1.0\Extest2.c */ #include <stdio.h> #include <std ...

  2. 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(二) controller

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第二十一章-授予身份与切换身份(二) 1.回顾 ...

  3. 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(一) table、entity、service、dao

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第二十一章 授予身份与切换身份(一) 1.使用场景 某个领导因为某 ...

  4. [core java学习笔记][第十一章异常断言日志调试]

    第11章 异常,断言,日志,调试 处理错误 捕获异常 使用异常机制的技巧 使用断言 日志 测试技巧 GUI程序排错技巧 使用调试器 11.1 处理错误 11.1.1异常分类 都继承自Throwable ...

  5. Flask 教程 第二十一章:用户通知

    本文翻译自The Flask Mega-Tutorial Part XXI: User Notifications 这是Flask Mega-Tutorial系列的第二十一章,我将添加一个私有消息功能 ...

  6. Flask 教程 第十一章:美化

    本文翻译自The Flask Mega-Tutorial Part XI: Facelift 这是Flask Mega-Tutorial系列的第十一部分,我将告诉你如何用基于Bootstrap用户界面 ...

  7. Python开发【十一章】:数据库操作Memcache、Redis

    一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...

  8. 进击的Python【第十一章】:消息队列介绍、RabbitMQ&Redis的重点介绍与简单应用

    消息队列介绍.RabbitMQ.Redis 一.什么是消息队列 这个概念我们百度Google能查到一大堆文章,所以我就通俗的讲下消息队列的基本思路. 还记得原来写过Queue的文章,不管是线程queu ...

  9. python 教程 第十七章、 网络编程

    第十七章. 网络编程 1)    FTP客户端 import ftplib import os import socket HOST = '127.0.0.1' DIRN = 'menus' FILE ...

随机推荐

  1. 【例题5-2 UVA - 101】The Blocks Problem

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用vector模拟就好. resize的时候,只是把多余的清理掉. 原先的不会变的. [错的次数] 在这里输入错的次数 [反思] 在 ...

  2. freemarker导出图片到word

    1.word模版制作: 在word中添加图片,第一步制作模板时,加入一张图片占位,然后打开xml文档,可以看到如下的一片base64编码后的代码: <w:binData w:name=" ...

  3. 从Lua调用C

    从Lua调用C: 方式:C函数从栈中获取函数參数(第一个參数总是局部栈的索引1),将结果压入栈中,C函数须要返回结果数量. 每一个函数都有自己的局部私有栈 样例: static int l_sin(l ...

  4. 二次封装CoreData

    (1)创建一个Data Model文件.命名为MyModel.xcdatamodeld (2)创建Users表,加入如图的字段 (3)创建NSManagedObject subclass表实体文件 ( ...

  5. adb常用命令 分类: H1_ANDROID 2013-09-08 15:22 510人阅读 评论(0) 收藏

    安装软件  adb install apk文件名称.apk  重新安装该软件  adb install -r apk文件名称.apk  卸载apk软件  adb uninstall apk包名.apk ...

  6. [Javascript] Combine Objects with Object.assign and Lodash merge

    Learn how to use Object.assign to combine multiple objects together. This pattern is helpful when wr ...

  7. hosts 持续更新 - laod

    https://laod.cn/hosts/2017-google-hosts.html 2017 Google hosts 持续更新[更新于:2017-08-22] 4,347 2,367,079 ...

  8. Facial keypoints detection Kaggle 竞赛系列

    3.2# Facial keypoints detection 作者:Stu. Rui QQ: 1026163725 原文链接:http://blog.csdn.net/i_love_home/art ...

  9. 【t082】牛跑步

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 新牛到部队,CG要求它们每天早上搞晨跑,从A农场跑到B农场.从A农场到B农场中有n-2个路口,分别标上 ...

  10. PatentTips - Systems, methods, and devices for dynamic resource monitoring and allocation in a cluster system

    BACKGROUND  1. Field  The embodiments of the disclosure generally relate to computer clusters, and m ...