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计算

一提到计算机,当然现在更多人把她叫做电脑,这两个词都是指computer。不管什么,只要提到她,普遍都会想到她能够比较快地做加减乘除,甚至乘方开方等。乃至于,有的人在口语中区分不开计算机...

python itchat给指定联系人发消息的方法

itchat模块 官方参考文档:https://itchat.readthedocs.io/zh/latest/ 安装 pip install itchat / pip3 insta...

pycharm设置注释颜色的方法

操作方法如下所示: File-->Settings-->Editor-->Color&Fonts-->LanguageDefaults-->Linecomm...

Python类的基础入门知识

复制代码 代码如下:class Account(object): "一个简单的类" account_type="Basic" def __init__(self,name,balance...

pandas数据清洗,排序,索引设置,数据选取方法

此教程适合有pandas基础的童鞋来看,很多知识点会一笔带过,不做详细解释 Pandas数据格式 Series DataFrame:每个column就是一个Series 基础属性shap...