`
tibaloga
  • 浏览: 872755 次
文章分类
社区版块
存档分类
最新评论

StandardError 和 LookupError

 
阅读更多

2011-3-1 更新。

赖勇浩(http://laiyonghao.com)

今晚没事,就读了一下 python manuals,找到了几个之前疏忽的知识点,其中就有 StandardError 和 LookupError。

StandardError

StandardError 继承自 Exception,它是除了 StopIteration、GeneratorExit、KeyboardInterrupt 和 SystemExit 之外的所有内置异常类的父类。之前我常用写这样的代码:

现在想来,其实是不对的,应该使用 StandardError 作为 MyError 的基类。另外,虽然我用 except Exception 这样的代码很少,不过在这里讲一下,大部分应该用 except StandardError。

LookupError

我不知道大家有没有写过这样的代码。。

某年某月某日,t 的类型从 list 之类的序列容器改成了 dict,太操蛋了,要把所有的 IndexError 都改为 KeyError。我做过几次这种事,当时把龟叔暗骂了若干次……好吧,现在我知道这是我的错了。LookupError 是 IndexError 和 KeyError 的基类,也就是说如果上面的代码是 except LookupError 的话,t 的类型变了也不用改代码。哇呒~

嗯,今晚看手册,除了这个收获以后,还有一些其它的知识点,不过现在太晚了,我改天再说吧。

嗯,真是温故知新啊,手册要多读!

分享到:
评论

相关推荐

    python搭建与量化金融

    1.描述python解决过程中错误Fatal Python error:init_sys_streams:can’t initialize sys standard streams LookupError: unknown encoding:65001;2 学习tushar获取股市数据,画图,适合学习python和入门量化金融

    Google C++ International Standard.pdf

    6.4 Name lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.5 Program and linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

    Android代码-Cacheable-Network

    Cacheable-Network A Cacheable Network Library For Android Application Intro ...Supports both (Standard): XML / JSON RESTful Service JSON-RPC Service Usage in your top level bui

    UNIX环境高级编程英文第三版+源码

    Contents Foreword to the Second Edition xix Preface xxi Preface to the Second Edition xxv ...B.2 Standard Error Routines 898 Appendix C. Solutions to Selected Exercises 905 Bibliography 947 Index 955

    vxworks_kernel_programmers_guide_6.9

    Error Detection and Reporting Policy Hooks ............................................... 18 Task Switch Hook Support .............................................................................. 18...

    VC++6.0核心编程源码.rar

    一般来说,该应用程序用于显示调试程序的Watch窗口和Error Lookup程序是如何运行的。当你启动该程序时,就会出现下面这个窗口。 见原书P9的插图 你可以将任何错误代码键入该编辑控件。当你单击Look Up按钮时,在...

    ICS delphixe10源码版

    ICS - Internet Component Suite - V8 - Delphi 7 to RAD Studio 10 Seattle ======================================================================= (Aka FPIETTE's Components) Revised: March 3, 2016 ...

    2009 达内Unix学习笔记

    命令和参数之间必需用空格隔开,参数和参数之间也必需用空格隔开。 一行不能超过256个字符;大小写有区分。 二、特殊字符含义 文件名以“.”开头的都是隐藏文件/目录,只需在文件/目录名前加“.”就可隐藏它。...

    The C programming Language(chm格式完整版)

    Table Lookup Typedef Unions Bit-fields Chapter 7: Input and Output Standard Input and Output Formatted Output - printf Variable-length Argument Lists Formatted Input - Scanf File Access ...

    UNIX Network Programming Volume 1, Third Edition (Unix网络编程卷1第3版英文版)

    Error Handling: Wrapper Functions Section 1.5. A Simple Daytime Server Section 1.6. Roadmap to Client/Server Examples in the Text Section 1.7. OSI Model Section 1.8. BSD Networking History ...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

    C++ 标准 ISO 14882-2011

    3.4 Name lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.5 Program and linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

    ImageMagick图片批量处理

    -dither method apply error diffusion to image -encoding type text encoding type -endian type endianness (MSB or LSB) of the image -family name render text with this font family -fill color color ...

    一个跨平台的CString源码

    // 2001-JUN-06 - I was violating the Standard name lookup rules stated // in [14.6.2(3)]. None of the compilers I've tried so // far apparently caught this but HP-UX aCC 3.30 did. The // ...

    The C programming Language

    referential Structures Table Lookup Typedef Unions Bit-fields <br>Chapter 7: Input and Output Standard Input and Output Formatted Output - printf Variable-length ...

    Absolute Database for D7

    including BLOB fields Full compatibility with standard DB-aware visual controls such as QuickReport, DBGrid, DBNavigator, DBImage, DBMemo, DBRichEdit and others Calculated and lookup fields Most of ...

    DevExpress VCL v2012 vol 1.6源码、例子、帮助

    B219006 - TcxBarEditItem - LookupComboBox - An error occurs when moving focus from one lookup bar item to another ExpressDocking Library Q440931 - TdxDockPanel is not activated by clicking the panel'...

    DevExpress VCL v2012 vol 1.6源码、例子、帮助-Part1

    B219006 - TcxBarEditItem - LookupComboBox - An error occurs when moving focus from one lookup bar item to another ExpressDocking Library Q440931 - TdxDockPanel is not activated by clicking the panel'...

    DevExpress VCL 2012 vol 1.6源码、例子、帮助-Part2

    B219006 - TcxBarEditItem - LookupComboBox - An error occurs when moving focus from one lookup bar item to another ExpressDocking Library Q440931 - TdxDockPanel is not activated by clicking the ...

Global site tag (gtag.js) - Google Analytics