不要嫌我啰嗦~继续话痨python
来源:互联网 发布:黎婴车祸多严重知乎 编辑:程序博客网 时间:2024/05/16 21:50
python的类支持多继承喔~
不过除非你十分熟悉多继承,否则应该尽量避免使用,因为据说会出现不可预见的麻烦。
多重继承的使用有一个需要特别注意的地方:如果一个方法从多个超类继承,那么必须注意一下超类的顺序,先继承的类会重写后继承滴类的方法。
好啦,类的东西先说到这里啦,下面说一说python的异常:
比嘟比嘟比嘟~
下面是一些重要的内建异常类:
Exception 所有异常的基类
AttributeError 特征引用或赋值失败时引发
IOError 试图打开不存在文件时引发
IndexError 使用序列中不存在的索引
KeyError 使用映射中不存在的键
NameError 找不到名字(变量)
SyntaxError 代码为错误形式
TypeError 内建操作或者函数应用于错误类型的对象
ValueError 内建操作或者函数应用于正确类型的对象,但是该对象使用不合适的值
ZeroDivisionError 除法或者模除的第二个参数为零
下面看一段捕捉一场的代码:
#catch exceptions:try: x = input('Enter the first Number:') y = input('Enter the second Number:') print x/yexcept ZeroDivisionError: print 'Error~Error~Error,the second number cant be zero!!!!!!!!!!'这样捕捉异常可以使得在用户错误操作时,程序输出一些比较友好的信息
import exceptionsprint dir(exceptions)#show all exceptions
这样可以输出系统中支持的所有异常:
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__doc__', '__name__', '__package__']
哇好多喔~~~~~~
下面是一个用类来实现的捕捉一样,可以通过类的属性,控制异常是否被捕捉:
#a new class for exceptionclass MuffledCalculator: muffled = False def calc(self, expr): try: return eval(expr) except ZeroDivisionError: if self.muffled: print "Division by zero is illegal" else: raisec = MuffledCalculator()print c.calc('10/5')c.muffled = Trueprint c.calc('10/0')c.muffled = Falseprint c.calc('10/0') print "Done!Tada!!"
结果:
2Division by zero is illegalNoneTraceback (most recent call last): File "Desktop/python猎人.spy/test.py", line 21, in <module> print c.calc('10/0') File "Desktop/python猎人.spy/test.py", line 7, in calc return eval(expr) File "<string>", line 1, in <module>ZeroDivisionError: integer division or modulo by zero
还可以捕捉多个异常:
#catch exceptions:try: x = input('Enter the first Number:') y = input('Enter the second Number:') print x/yexcept ZeroDivisionError: print 'Error~Error~Error,the second number cant be zero!!!!!!!!!!'except TypeError: print 'that wasnt a number,was it?'
下面这个方式,可以不妨碍程序继续运行,而且可以记录下错误:
#catch exceptions:try: x = input('Enter the first Number:') y = input('Enter the second Number:') print x/yexcept (ZeroDivisionError,TypeError),e: print e
真正的全捕捉和else语句:
#catch exceptions:try: x = input('Enter the first Number:') y = input('Enter the second Number:') print x/yexcept : print "something wrong"else: print 'well well'如果一切正常,木有异常出现,那么就会执行else语句的内容。
全捕捉的做法其实很危险,因为这样会隐藏程序员未想到并且木有做好准备处理的错误。
另外,还有finally可用,finally子句一定会被执行,不管有木有异常发生,该子句可以用来关闭文件或者网络套接字:
#catch exceptions:try: x = input('Enter the first Number:') y = input('Enter the second Number:') print x/yexcept (Exception),e: print "something wrong:",eelse: print 'well well,every thing seems allright'finally: print 'ok good night'
据说,使用try/except语句比使用if/else语句更“python化“一些~
ok今天就到这里啦~~~兔几要去蹦哒一会儿了~~~
- 不要嫌我啰嗦~继续话痨python
- 零基础学Python之四啰嗦的除法
- 继续Python
- JDK8的随笔(05)_Method References的种类继续啰嗦一下
- 希望自己不要继续懈怠
- 转: HP大中华区总裁孙振耀退休感言--你没有读,继续走弯路的你不要怪我
- 啰嗦一个
- 啰嗦编码
- 谁能让我继续?
- 继续我的博客
- OI继续,我还没走
- 大家早安,唠叨的兔几出现了,继续话痨python
- 不够优秀就不要腆着脸继续占便宜
- MFC到底还要不要继续学习
- 继续学python
- 继续说python类~
- DAY4继续python入门
- 我自己整理的一点python的学习笔记。后续继续更新
- Baidu WIFI Music Box
- android书
- jquery.validate 一些基本使用
- 删除mysql
- ubuntu 14.04 下安装Armadillo库 进行稀疏矩阵特征分解
- 不要嫌我啰嗦~继续话痨python
- TCP/IP Socket Communications in MATLAB example
- iOS8的三种分辨率
- leetcode38 Count and Say
- App研发录:架构设计、Crash分析和竞品技术分析——互动出版网
- Mac终端命令远程开启屏幕共享进行远程控制
- 浅析在对象中析构函数为何定义为虚函数
- 键盘弹出的响应事件
- Android存储