Home > Backend Development > Python Tutorial > What types of exceptions are there in Python? Introduction to python exception handling

What types of exceptions are there in Python? Introduction to python exception handling

不言
Release: 2018-09-20 15:54:01
Original
7441 people have browsed it

What this article brings to you is what types of exceptions are there in python? The introduction to Python exception handling has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Exception handling is a mechanism in a programming language or computer hardware that is used to handle abnormal conditions that occur in software or information systems (that is, certain special conditions that exceed the normal execution flow of the program).

1. Types of exceptions

There are many types of exceptions. Common exceptions include:

AttributeError 试图访问一个对象没有的属性,比如foo.x,但是foo没有属性x
IOError 输入/输出异常;基本上是无法打开文件
ImportError 无法引入模块或包;基本上是路径问题或名称错误
IndentationError 语法错误(的子类) ;代码没有正确对齐
IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5]
KeyError 试图访问字典里不存在的键
KeyboardInterrupt Ctrl+C被按下
NameError 尝试访问一个没有申明的变量
SyntaxError Python代码非法,代码不能编译(个人认为这是语法错误,写错了)
TypeError 传入对象类型与要求的不符合
UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量,导致你以为正在访问它
ValueError 传入一个调用者不期望的值,即使值的类型是正确的
Copy after login

Other exceptions:

Exception can catch any exception

BaseException    所有异常的基类
SystemExit    解释器请求退出
KeyboardInterrupt    用户中断执行(通常是输入^C)
Exception    常规错误的基类
StopIteration    迭代器没有更多的值
GeneratorExit    生成器(generator)发生异常来通知退出
StandardError    所有的内建标准异常的基类
ArithmeticError    所有数值计算错误的基类
FloatingPointError    浮点计算错误
OverflowError    数值运算超出最大限制
ZeropisionError    除(或取模)零 (所有数据类型)
AssertionError    断言语句失败
AttributeError    对象没有这个属性
EOFError    没有内建输入,到达EOF 标记
EnvironmentError    操作系统错误的基类
IOError    输入/输出操作失败
OSError    操作系统错误
WindowsError    系统调用失败
ImportError    导入模块/对象失败
LookupError    无效数据查询的基类
IndexError    序列中没有此索引(index)
KeyError    映射中没有这个键
MemoryError    内存溢出错误(对于Python 解释器不是致命的)
NameError    未声明/初始化对象 (没有属性)
UnboundLocalError    访问未初始化的本地变量
ReferenceError    弱引用(Weak reference)试图访问已经垃圾回收了的对象
RuntimeError    一般的运行时错误
NotImplementedError    尚未实现的方法
SyntaxError    Python 语法错误
IndentationError    缩进错误
TabError    Tab 和空格混用
SystemError    一般的解释器系统错误
TypeError    对类型无效的操作
ValueError    传入无效的参数
UnicodeError    Unicode 相关的错误
UnicodeDecodeError    Unicode 解码时的错误
UnicodeEncodeError    Unicode 编码时错误
UnicodeTranslateError    Unicode 转换时错误
Warning    警告的基类
DeprecationWarning    关于被弃用的特征的警告
FutureWarning    关于构造将来语义会有改变的警告
OverflowWarning    旧的关于自动提升为长整型(long)的警告
PendingDeprecationWarning    关于特性将会被废弃的警告
RuntimeWarning    可疑的运行时行为(runtime behavior)的警告
SyntaxWarning    可疑的语法的警告
UserWarning    用户代码生成的警告
Copy after login

##2. Catch exceptions and handle them Exception

#Exception handling can only be performed if the corresponding exception type is captured

Exception capture and processing method:

try:
    #程序执行的代码,异常检测的代码
    pass
except Exception as e:
    #捕获异常后进行的代码,
    print(e)
    pass
else:
    #没有捕获到相应的异常后进行的代码
    pass
finally:
    #不论有没有异常捕获最后都要执行的代码
    pass
Copy after login

The except here is like catch in java

You can also add multiple except statements in the code to catch different types of exceptions and execute different Operation

3. Actively throw an exception

Exception throwing can be added manually

try:
    raise Exception("我自己抛的")
except Exception as e:
    print(e)#这里会输出“我自己抛的”
Copy after login

4. Custom exception type

We can define the type of exception we need

Mainly use the __str__ method in the class to achieve

class myException(BaseException):
    def __init__(self,message):
        self.message=message
    def __str__(self):
        return self.message

try:
    raise myException('自定义的异常类型')
except myException as e:
    print(e) 
Copy after login

5. Assertion

Assertion assert is used to determine whether a certain condition is true. If it is not true, an AssertionError exception is thrown. This exception can be captured

##
assert 条件
#相当于
if not 条件 :
    raise AssertionError()

assert 条件,"描述"
#相当于
if not 条件 :
    raise AssertionError("描述")
Copy after login

The above is the detailed content of What types of exceptions are there in Python? Introduction to python exception handling. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template