Python Exception Name

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. In general, when a Python script encounters a situation that it can't cope with, it raises an exception. An exception is a Python object that represents an error.

When a Python script raises an exception, it must either handle the exception immediately otherwise it would terminate and come out.

EXCEPTION NAME DESCRIPTION
Exception Base class for all exceptions
StopIteration Raised when the next() method of an iterator does not point to any object.
SystemExit Raised by the sys.exit() function.
StandardError Base class for all built-in exceptions except StopIteration and SystemExit.
ArithmeticError Base class for all errors that occur for numeric calculation.
OverflowError Raised when a calculation exceeds maximum limit for a numeric type.
FloatingPointError Raised when a floating point calculation fails.
ZeroDivisonError Raised when division or modulo by zero takes place for all numeric types.
AssertionError Raised in case of failure of the Assert statement.
AttributeError Raised in case of failure of attribute reference or assignment.
EOFError Raised when there is no input from either the raw_input() or input() function and the end of file is reached.
ImportError Raised when an import statement fails.
KeyboardInterrupt Raised when the user interrupts program execution, usually by pressing Ctrl+c.
LookupError Base class for all lookup errors.
IndexError Raised when an index is not found in a sequence.
KeyError Raised when the specified key is not found in the dictionary.
NameError Raised when an identifier is not found in the local or global namespace.
UnboundLocalError Raised when trying to access a local variable in a function or method but no value has been assigned to it.
EnvironmentError Base class for all exceptions that occur outside the Python environment.
IOError Raised when an input/ output operation fails, such as the print statement or the open() function when trying to open a file that does not exist.
OSError Raised for operating systemrelated errors.
SyntaxError Raised when there is an error in Python syntax.
IndentationError Raised when indentation is not specified properly.
SystemError Raised when the interpreter finds an internal problem, but when this error is encountered the Python interpreter does not exit.
SystemExit Raised when Python interpreter is quit by using the sys.exit() function. If not handled in the code, causes the interpreter to exit.
TypeError Raised when an operation or function is attempted that is invalid for the specified data type.
ValueError Raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified.
RuntimeError Raised when a generated error does not fall into any category.
NotImplementedError Raised when an abstract method that needs to be implemented in an inherited class is not actually implemented.

Ref: http://www.tutorialspoint.com/python/python_exceptions.htm


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的异常是指在程序执行过程中发生的错误或异常情况。当出现异常时,程序会中断执行,并根据异常类型执行相应的处理操作。Python提供了一些内置的异常类型,同时也可以自定义异常。 常见的异常类型包括: - `SyntaxError`:语法错误 - `IndentationError`:缩进错误 - `NameError`:变量名错误 - `TypeError`:类型错误 - `ValueError`:值错误 - `ZeroDivisionError`:除零错误 - `FileNotFoundError`:文件未找到错误 - `IndexError`:索引错误 - `KeyError`:键错误 - `AssertionError`:断言错误 当出现异常时,可以使用`try-except`语句捕获异常并进行处理。示例如下: ```python try: # 可能会出现异常的代码块 result = 10 / 0 # 除零错误 except ZeroDivisionError: # 异常处理代码块 print("除以零错误发生!") ``` 除了捕获特定类型的异常外,还可以使用`except`语句捕获所有异常,如: ```python try: # 可能会出现异常的代码块 result = 10 / 0 # 除零错误 except Exception as e: # 异常处理代码块 print("发生异常:", str(e)) ``` 这样可以捕获所有类型的异常并将异常信息打印出来。同时,还可以使用`finally`语句定义无论是否发生异常都会执行的代码块。 除了使用`try-except`语句处理异常外,还可以使用`raise`语句手动抛出异常。示例如下: ```python def divide(a, b): if b == 0: raise ValueError("除数不能为零!") return a / b try: result = divide(10, 0) except ValueError as e: print(str(e)) ``` 这样可以在特定的条件下手动抛出异常,以便进行相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值