如果你需要自定义异常的话,可以从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
相关推荐
本实例是一个关于python自定义异常的实例,供大家学习和交流
yichang.py,python自定义异常学习代码
python自定义异常实例详解 本文通过两种方法对Python 自定义异常进行讲解,第一种:创建一个新的exception类来拥有自己的异常,第二种:raise 唯一的一个参数指定了要被抛出的异常 1、可以通过创建一个新的...
今天小编就为大家分享一篇python 自定义异常和异常捕捉的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
主要介绍了详解在Python程序中自定义异常的方法,是Python入门学习中的基础知识,需要的朋友可以参考下
那么我们编写一个模块也可以为使用此模块者划定红线,来约束使用者可用哪些数据,这就需要自定义异常类型。 首先需要了的是自定义类是的父类: python说明文档中是这样说 exception Exception: 所有内置的非系统...
主要介绍了python异常处理、自定义异常、断言,结合实例形式分析了python异常处理、自定义异常、断言相关概念、原理、用法及操作注意事项,需要的朋友可以参考下
主要介绍了Python异常继承关系和自定义异常实现代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
自定义异常类 虽然内建异常类中已经包括了许多的错误情况,但是在项目的开发过程中还是要根据实际情况,创建具有自己特色的异常处理类,需要让自定义异常类继承“Exception”类或其他内建异常类即可。 语法格式: ...
Python 异常机制 09_自定义异常类_raise抛出异常.mp4
用户密码的合法化验证,要求密码由4到6个数字组成,若长度不落在这个范围或不是由数字组成,抛出自己的异常。
01 异常简介 02 异常类 03 异常处理 目录页 04 抛出异常 05 自定义异常 06 with和as环境安装器 精品课件----Python常见异常处理全文共71页,当前为第3页。 异常介绍 在Python中,程序在执行的过程中产生的错误称为...
主要介绍了详解python中自定义超时异常的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
python烟花代码-24-自定义异常代码实现之异常类.ev4.rar
python烟花代码-25-自定义异常代码实现之捕获异常.ev4.rar
异常触发:使用raise命令抛出异常,即可使用异常基类Exception,也可使用自定义异常类(继承Exception类)。 class Point: def __init__(self, x, y): self.x = x self.y = y # Define a class to raise Line ...