python异常触发及自定义异常类解析

yipeiwu_com6年前Python基础

python程序运行中,可由程序抛出异常。

异常触发:使用raise命令抛出异常,即可使用异常基类Exception,也可使用自定义异常类(继承Exception类)。

class Point:
  def __init__(self, x, y):
    self.x = x
    self.y = y
# Define a class to raise Line errors
class LineError(Exception):  #继承自基类Exception
  def __init__(self,ErrorInfo):
    self.errorinfo=ErrorInfo
  def __str__(self):
    return self.errorinfo
class Line:
  def __init__(self, point1, point2):
    self.point1 = point1
    self.point2 = point2
    if point1.x==point2.x and point1.y==point2.y:
      raise LineError("Cannot create line") 
line = Line(Point(1, 2), Point(1, 2))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python生成随机密码或随机字符串的方法

本文实例讲述了python生成随机密码或随机字符串的方法。分享给大家供大家参考。具体实现方法如下: import string,random def makePassword(mi...

从django的中间件直接返回请求的方法

实例如下所示: #coding=utf-8 import json import gevent from django.http import HttpResponse from s...

Python 中的lambda函数介绍

Lambda函数,即Lambda 表达式(lambda expression),是一个匿名函数(不存在函数名的函数),Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambd...

对Python 语音识别框架详解

如下所示: from win32com.client import constants import os import win32com.client import pythonc...

Python实现的简单算术游戏实例

本文实例讲述了Python实现的简单算术游戏。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python from operator import add,...