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

小鱼儿 2023-03-05 09:53 141阅读 0赞

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语句的基本语法是:

  1. try:
  2. #statement
  3. except typeofError:
  4. #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语句进行异常处理示例

  1. # input two integer numbers
  2. a=int(input('Enter value of a: '))
  3. b=int(input('Enter value of b: '))
  4. try:
  5. print('a/b=',a/b)
  6. except ZeroDivisionError:
  7. print("Sorry, wrong value of the denominator.")

Output

输出量

  1. RUN 1:
  2. Enter value of a: 3
  3. Enter value of b: 5
  4. a/b= 0.6
  5. RUN 2:
  6. Enter value of a: 3
  7. Enter value of b: 0
  8. Sorry, wrong value of the denominator.

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

python中异常处理

发表评论

表情:
评论列表 (有 0 条评论,141人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python异常处理

    异常处理是在编程过程中处理可能出现的错误或异常情况的一种机制。当程序出现错误时,如果没有适当的异常处理,程序可能会崩溃或产生不可预测的结果。Python提供了一套强大的异常处理

    相关 python异常处理

    python中的异常 异常是指程序中的例外,违例情况。异常机制是指程序出现错误后,程序的处理方法。当出现错误后,程序的执行流程发生改变,程序的控制权转移到异常处理。