python 异常 try-except句型
来源:互联网 发布:矩阵秩的性质 编辑:程序博客网 时间:2024/05/29 00:32
1.句型
try:
表达式 1(如果表达式,可以成功执行,则执行,跳到 finally 语句)
except ExpectErrorType, Argument: (表达式1 没有做成功,且正好是 ExpectErrorType 的错误情况,则执行)
表达式2 (如何处理这种异常情况)
else: (try succ && 上面 except 语句任一满足 )之外的情况处理方法
.....
finally:
.... 无论什么情况都会的处理
2.except 子句解释
except ExceptErrorType , Argument:
ExceptErrorType: 期待的错误类型
Argument 是异常类的实例, 包含来自异常代码的诊断信息。如果你捕获了一个异常,你就可以通过这个异常类的实例来获取更多的关于这个异常的信息。
- >>> try:
- ... 1/0
- ... except ZeroDivisionError,reason:
- ... pass
- ...
- >>> type(reason)
- <type 'exceptions.ZeroDivisionError'>
- >>> print reason
- integer division or modulo by zero
- >>> reason
- ZeroDivisionError('integer division or modulo by zero',)
- >>> reason.__class__
- <type 'exceptions.ZeroDivisionError'>
- >>> reason.__class__.__doc__
- 'Second argument to a division or modulo operation was zero.'
- >>> reason.__class__.__name__
- 'ZeroDivisionError'
3.多个异常:
1)写多个except 子句
2)一个except 子句传多个参数
e.g
- try:
- floatnum = float(raw_input("Please input a float:"))
- intnum = int(floatnum)
- print 100/intnum
- except ZeroDivisionError:
- print "Error:you must input a float num which is large or equal then 1!"
- except ValueError:
- print "Error:you must input a float num!"
- [root@]# python test.py
- Please input a float:fjia
- Error:you must input a float num!
- [root@]# python test.py
- Please input a float:0.9999
- Error:you must input a float num which is large or equal then 1!
- [root@]# python test.py
- Please input a float:25.091
- 4
4.异常情况 ExceptErrorType
BaseException+-- SystemExit+-- KeyboardInterrupt+-- GeneratorExit+-- Exception+-- StopIteration+-- StandardError| +-- BufferError| +-- ArithmeticError| | +-- FloatingPointError| | +-- OverflowError| | +-- ZeroDivisionError 除数为0| +-- AssertionError| +-- AttributeError 尝试访问未知的对象属性| +-- EnvironmentError| | +-- IOError 输入输出错误(比如你要读的文件不存在)| | +-- OSError| | +-- WindowsError (Windows)| | +-- VMSError (VMS)| +-- EOFError| +-- ImportError| +-- LookupError| | +-- IndexError 索引超出序列范围| | +-- KeyError 请求一个不存在的字典关键字| +-- MemoryError| +-- NameError 尝试访问一个没有申明的变量| | +-- UnboundLocalError| +-- ReferenceError| +-- RuntimeError| | +-- NotImplementedError| +-- SyntaxError 语法错误| | +-- IndentationError| | +-- TabError| +-- SystemError| +-- TypeError| +-- ValueError 传给函数的参数类型不正确,比如给int()函数传入字符串形| +-- UnicodeError| +-- UnicodeDecodeError| +-- UnicodeEncodeError| +-- UnicodeTranslateError+-- Warning+-- DeprecationWarning+-- PendingDeprecationWarning+-- RuntimeWarning+-- SyntaxWarning+-- UserWarning+-- FutureWarning+-- ImportWarning+-- UnicodeWarning+-- BytesWarning
0 0
- python 异常 try-except句型
- python 异常 try-except句型
- Python 异常 try except
- Python 异常处理 try/except
- python异常处理try...except
- Python 异常处理 Python 基础教程 try..except
- (Python)异常处理try...except、raise
- Python文件异常try/except/finally
- (Python)异常处理try...except、raise
- python中的异常处理 try..except
- Python异常处理try...except、raise
- python try except 捕获所有异常
- Python---异常处理try...except、raise
- (Python)异常处理try...except、raise
- Python异常处理try...except、raise
- python try...except....else...finally处理异常
- Python 异常处理 (try 和 except)
- try,except异常处理
- 开启ssh服务
- 详解window.history
- Shell排序
- android中File文件的读的实现
- Distinctive Image Features from Scale-Invariant Keypoints(SIFT)
- python 异常 try-except句型
- Linux下的多路复用和unix套接字的综合使用
- Mantle
- python开发环境搭建
- 用Beyond Compare怎么修改对比文件颜色
- 动态规划 矩阵连乘问题
- IOS ZipArchive
- 日志
- 十大渗透测试演练系统