python中异常处理_Python中的异常处理

python中异常处理

Python中的异常 (Exception in Python)

An exception is a Python object that represents error that occurs during the execution of the program and this disturbs the flow of a program. In general, if Python does not encounter any scripts or lines of code then it raises a type of exception.

一个例外是Python对象,该对象表示在程序执行期间发生的错误,这会干扰程序的流程。 通常,如果Python没有遇到任何脚本或代码行,那么它将引发一种异常。

Python的常见异常 (Common Exceptions in Python)

Some of the most common built-in exceptions in Python are,

Python中一些最常见的内置异常是,

  1. ValueError:

    ValueError:

    This type of error occurs when we have written an argument or line of code that has the right type but inappropriate value. For example, a variable is assigned with the string and we are trying to convert it into integer then we will get this type of error.

    当我们编写的参数或代码行的类型正确但值不合适时,就会发生这种类型的错误。 例如,为变量分配了字符串,而我们尝试将其转换为整数,则将得到这种类型的错误。

  2. ZeroDivisionError:

    ZeroDivisionError:

    This is the most common type of error which occurs when we are trying to divide or taking modulo of any integer by the Zero. For example, a variable x by 5 and we are trying to do operation x%0 or x/0 then this shows us ZeroDivisionError.

    这是最常见的错误类型,当我们尝试将任何整数除以零或取零为模时,会发生这种错误。 例如,将变量x乘以5,然后我们尝试执行x%0或x / 0运算,则向我们显示ZeroDivisionError。

  3. ImportError:

    ImportError:

    This error occurs when we are trying to import a module in the program but it not found. For example, if we are importing a module like Bipin in the program then it shows this type of error.

    当我们尝试在程序中导入模块但未找到时,会发生此错误。 例如,如果我们要在程序中导入像Bipin这样的模块,则它将显示这种类型的错误。

  4. IndentionError:

    IndentionError:

    As we all know that the Python follows the rule of indentation. So, when we are going to write something out of indentation then this type of error will occur.

    众所周知,Python遵循缩进规则。 因此,当我们要从缩进中写一些东西时,就会发生这种类型的错误。

  5. SyntaxError:

    语法错误:

    This error occurs when we have written any incorrect syntax in the program. As we all know that the keyword 'int' is used to convert the input into an integer type but when we are trying to use 'Int' instead of 'int' then it will show the SyntaxError.

    当我们在程序中编写了任何不正确的语法时,就会发生此错误。 众所周知,关键字“ int”用于将输入转换为整数类型,但是当我们尝试使用“ Int”而不是“ int”时,它将显示SyntaxError。

  6. IndexError:

    IndexError:

    This type of error occurs when you are trying to access any value which is out of the given range. For example, we have created a list of lengths of 5 but we are trying to access the element at index 6 then it will show the IndexError.

    当您尝试访问超出给定范围的任何值时,会发生此类错误。 例如,我们创建了一个长度为5的列表,但是我们试图访问索引为6的元素,那么它将显示IndexError。

  7. MemoryError:

    MemoryError:

    This type of error occurs when an operation runs out of memory.

    当操作内存不足时,会发生此类错误。

To overcome these types of exceptions in python programming language, we use the try-except statements.

为了克服python编程语言中的这些类型的异常 ,我们使用try-except语句

The basic syntax of try-except statement is,

try-except语句的基本语法是:

try:
   #statement
except typeofError:
   #statement

Algorithm of try-except statement

try-except语句的算法

  • If the code or statement provided in the try block has no exception then only try blocks will be executed.

    如果try块中提供的代码或语句没有异常,则仅将执行try块。

  • If any exception occurs in the block of try then try block skipped and except block will be executed.

    如果try块中发生任何异常,则将跳过try块,将执行except块。

Now, we will see an example of exception handling by using the try-except statement in Python.

现在,我们将看到一个使用Python中的try-except语句进行异常处理示例

# input two integer numbers
a=int(input('Enter value of a: '))
b=int(input('Enter value of b: '))

try:
    print('a/b=',a/b)
except ZeroDivisionError:
    print("Sorry, wrong value of the denominator.")

Output

输出量

RUN 1:
Enter value of a: 3
Enter value of b: 5
a/b= 0.6

RUN 2:
Enter value of a: 3
Enter value of b: 0
Sorry, wrong value of the denominator.


翻译自: https://www.includehelp.com/python/exceptions-handling.aspx

python中异常处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值