笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

8.5. 用户自定义异常

发布者: 三寸日光

在程序中可以通过创建新的异常类型来命名自己的异常(Python 类的内容请参见  )。异常类通常应该直接或间接的从 Exception 类派生,例如:

>>> class MyError(Exception):
...     def __init__(self, value):
...         self.value = value
...     def __str__(self):
...         return repr(self.value)
...
>>> try:
...     raise MyError(2*2)
... except MyError as e:
...     print 'My exception occurred, value:', e.value
...
My exception occurred, value: 4
>>> raise MyError('oops!')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
__main__.MyError: 'oops!'

在这个例子中,Exception 默认的 __init__() 被覆盖。新的方式简单的创建 value 属性。这就替换了原来创建 args 属性的方式。

异常类中可以定义任何其它类中可以定义的东西,但是通常为了保持简单,只在其中加入几个属性信息,以供异常处理句柄提取。如果一个新创建的模块中需要抛出几种不同的错误时,一个通常的作法是为该模块定义一个异常基类,然后针对不同的错误类型派生出对应的异常子类:

class Error(Exception):
    """Base class for exceptions in this module."""
    pass

class InputError(Error):
    """Exception raised for errors in the input.

    Attributes:
        expression -- input expression in which the error occurred
        message -- explanation of the error
    """

    def __init__(self, expression, message):
        self.expression = expression
        self.message = message

class TransitionError(Error):
    """Raised when an operation attempts a state transition that's not
    allowed.

    Attributes:
        previous -- state at beginning of transition
        next -- attempted new state
        message -- explanation of why the specific transition is not allowed
    """

    def __init__(self, previous, next, message):
        self.previous = previous
        self.next = next
        self.message = message

与标准异常相似,大多数异常的命名都以 “Error” 结尾。

最新评论

Python3编程手册
  1. Python 入门指南

  2. 1. 开胃菜

  3. 2.1. 调用 Python 解释器

  4. 2.2. 解释器及其环境

  5. 3. Python 简介

  6. 3.1. 将 Python 当做计算器

  7. 3.2. 编程的第一步

  8. 4. 深入 Python 流程控制

  9. 4.1. if 语句

  10. 4.2. for 语句

  11. 4.3. range() 函数

  12. 4.4. break 和 continue 语句

  13. 4.5. pass 语句

  14. 4.6. 定义函数

  15. 4.7. 深入 Python 函数定义

  16. 4.8. 插曲:编码风格

  17. 5. 数据结构

  18. 5.1. 关于列表更多的内容

  19. 5.2. del 语句

  20. 5.3. 元组和序列

  21. 5.4. 集合

  22. 5.5. 字典

  23. 5.6. 循环技巧

  24. 5.7. 深入条件控制

  25. 5.8. 比较序列和其它类型

  26. 6. 模块

  27. 6.1. 深入模块

  28. 6.2. 标准模块

  29. 6.3. dir() 函数

  30. 6.4. 包

  31. 7. 输入和输出

  32. 7.1. 格式化输出

  33. 7.2. 文件读写

  34. 8. 错误和异常

  35. 8.1. 语法错误

  36. 8.2. 异常

  37. 8.3. 异常处理

  38. 8.4. 抛出异常

  39. 8.5. 用户自定义异常

  40. 8.6. 定义清理行为

  41. 8.7. 预定义清理行为

  42. 9. 类

  43. 9.1. 术语相关

  44. 9.2. Python 作用域和命名空间

  45. 9.3. 初识类

  46. 9.4. 一些说明

  47. 9.5. 继承

  48. 9.6. 私有变量

  49. 9.7. 补充

  50. 9.8. 异常也是类

  51. 9.9. 迭代器

  52. 9.10. 生成器

  53. 9.11. 生成器表达式

  54. 10. Python 标准库概览

  55. 10.1. 操作系统接口

  56. 10.2. 文件通配符

  57. 10.3. 命令行参数

  58. 10.4. 错误输出重定向和程序终止

  59. 10.5. 字符串正则匹配

  60. 10.6. 数学

  61. 10.7. 互联网访问

  62. 10.8. 日期和时间

  63. 10.9. 数据压缩

  64. 10.10. 性能度量

  65. 10.11. 质量控制

  66. 10.12. “瑞士军刀”

  67. 11. 标准库浏览

  68. 11.1. 输出格式

  69. 11.2. 模板

  70. 11.3. 使用二进制数据记录布局

  71. 11.4. 多线程

  72. 11.5. 日志

  73. 11.6. 弱引用

  74. 11.7. 列表工具

  75. 11.8. 十进制浮点数算法

  76. 12. 接下来?

  77. 13. 交互式输入行编辑历史回溯

  78. 13.1. 行编辑

  79. 13.2. 历史回溯

  80. 13.3. 快捷键绑定

  81. 13.4. 其它交互式解释器

  82. 14. 浮点数算法:争议和限制

  83. 14.1. 表达错误

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-5-20 09:17 , Processed in 0.016173 second(s), 18 queries .

© 2001-2020

返回顶部