python 自定义异常的定义和代码
来源:互联网 发布:淘宝社区首页 编辑:程序博客网 时间:2024/05/21 14:03
转自:http://www.cnpythoner.com/post/292.html#
如果你需要自定义异常的话,可以从Exception类派生。 在这个例子中,默认的__init__()异常已被我们重写。
>>> 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!'
... 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!'
常见的做法是创建一个由该模块定义的异常基类和子类,创建特定的异常类不同的错误条件。
我们通常定义的异常类,会让它比较简单,允许提取异常处理程序的错误信息,当创建一个异常模块的时候,常见的做法是创建一个由该模块定义的异常基类和子类,根据不同的错误条件,创建特定的异常类:
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
"""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
0 0
- python 自定义异常的定义和代码
- 定义自定义的异常
- Python 练习代码 -- 异常,抛异常, 自定义异常
- python 自定义异常和异常捕捉
- python自定义异常和主动抛出异常(raise)
- Python-Python 3.x下自定义异常抛出和接收多个值的技巧
- AJAX.NET自定义异常的代码示例
- Python 实现简单的自定义异常类型
- PYTHON 自定义异常类的使用
- 《第一行代码》学习笔记-- 基础的自定义标题控件的定义和使用
- 异常的定义、抛出和处理
- 异常处理和异常的代码优化
- Python自定义异常
- Python 自定义异常
- Python-自定义异常
- Python:自定义异常类
- python 自定义异常
- Python如何自定义异常
- 《GitHub入门与实践》
- ubantu下安装搜狗输入法
- hdu 5289 Assignment 【ST算法】
- C语言编程基础-16动态内存分配 二级指针做形参 函数指针
- Ferry Loading IV(队列)
- python 自定义异常的定义和代码
- 在人机交互实验中遇到的一些问题
- python中的三元运算
- 【UIKit】-11-UIAppearance - 全局设置,整个 app 统一
- leetCode 77.Combinations (组合)
- HDU 1969 Pie
- 数据结构算法代码
- 为Symfony2和Redis正名,基于PHP的10亿请求/周网站打造
- 正则表达式删除注释及空白行