python错误与异常处理_python错误类型事物中的异常处理

python错误与异常处理

程式设计(Programming)

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. In this article, you will learn about different types of errors and exceptions that are built into Python. They are raised whenever the Python interpreter encounters errors.

异常(或例外事件)是在程序执行期间出现的问题。 当发生异常时,程序的正常流程将中断,程序/应用程序将异常终止,因此不建议这样做,因此,应对这些异常进行处理。 在本文中,您将了解Python内置的不同类型的错误和异常。 每当Python解释器遇到错误时,就会引发它们。

Generally, there are three types of errors:

通常,存在三种类型的错误:

  1. Compile-time error: Suppose we have a piece of code and it has multiple statements. In one of the statements, we have a syntax error.

    编译时错误:假设我们有一段代码,并且有多个语句。 在其中一条语句中,我们遇到语法错误。

Example: We are working on if block and forget the colon(:) in the last or we are working on print function with wrong spelling. Then these types of mistakes come in syntax error under compile-time errors.

示例:我们正在研究if块,最后忘记了冒号(:),或者我们正在使用错误的拼写进行打印功能。 然后,这些类型的错误会在编译时错误变成语法错误

2. Logical error: Suppose the code is working, nothing wrong with the syntax. Assume we want to add two numbers 3+3 and then the answer comes 5 (wrong output) so, it is a logical error. In logical error, the code gets compiled and codes also give the output but the problem is it is not the correct answer.

2.逻辑错误:假设代码正在运行,语法没有问题。 假设我们要添加两个数字3 + 3,然后答案为5(错误的输出),所以这是一个逻辑错误。 在逻辑错误中,代码被编译,代码也给出输出,但是问题是它不是正确的答案。

3. Run time error: In this type of error, the code will compile, no syntactical error, the code will not give the logical error as well. But sometimes the user might give the wrong input means if the user divide number 6 with 0 that where it will give you error e.g error name: divide by zero. Somewhere between the code, the error is coming that a run time error.

3.运行时错误:在这种类型的错误中,代码将编译,没有语法错误,代码也不会给出逻辑错误。 但是有时,如果用户将数字6除以0可能会给您错误,例如,错误名称:除以零,用户可能会输入错误的输入方式 在代码之间的某个地方,错误即将发生,即运行时错误。

The main logic is not to get code stop execution because of error.

主要逻辑是不要使代码因错误而停止执行。

Example:

例:

Let us take two numbers and perform a division method on them. The second time we didn’t get the ‘Hello World’ in the output, so the error occurred somewhere in between.

让我们取两个数字并对它们执行除法。 第二次我们在输出中没有得到“ Hello World”,因此错误发生在两者之间。

a,b = 5,2
print(a/b)
print('Hello World')#output:
2.5 #everything fine here, no error
Hello World# change the value of b to zero
a,b = 5,0
print(a/b)
print('Hello World')#output:
ZeroDivisionError: division by zero #error occured

To solve this kind of error/exception, we use special blocks ‘try and except’. Where we feel that this statement giving some error then, we keep it in a try block because that statement is a critical statement.

为了解决这种错误/异常,我们使用特殊的块“ try andexcept”。 然后,如果我们认为该语句给出了一些错误,则将其保留在try块中,因为该语句是关键语句。

#With try and except block
a,b = 5,0try: #getting error
print(a/b)
except Exception: #handling error
print("You cannot divide with zero')print('Hello World')#output:
You cannot divide with zero
Hello World

The try statement works as follows.

try语句的工作方式如下。

  • First, the try clause (the statement(s) between the try and except keywords) is executed.

    首先,执行try子句( tryexcept关键字之间的语句)。

  • If no exception occurs, the except clause is skipped and execution of the try statement is finished.

    如果没有异常发生,将跳过except子句,并结束try语句的执行。

  • If an exception occurs during the execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.

    如果在try子句执行期间发生异常,则将跳过该子句的其余部分。 然后,如果其类型与except关键字命名的异常匹配,则执行except子句,然后在try语句之后继续执行。

  • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.

    如果发生与except子句中命名的异常不匹配的异常,则将其传递给外部try语句; 如果未找到处理程序,则它是未处理的异常,并且执行停止并显示一条消息,如上所示。

If we want to see what type of error occurred then we write as:

如果我们想看看发生了什么类型的错误,那么我们写成:

#With try and except block
a,b = 5,0try: #getting error
print(a/b)
except Exception as e: #handling error
print("You cannot divide with zero-', e)#output:
You cannot divide with zero- division by zero
Hello World

Conclusion:

结论:

The Python interpreter raises an exception each time it detects an error in an expression or statement. Users can also raise exceptions with raise and assert statements. Raising exceptions. An exception is an object instance with a class that inherits, either directly or indirectly, from the BaseException class. An Error might indicate critical problems that a reasonable application should not try to catch, while an Exception might indicate conditions that an application should try to catch. Errors are a form of an unchecked exception and are irrecoverable like an OutOfMemoryError, which a programmer should not try to handle.

每次解释器或表达式中的错误时,Python解释器都会引发异常。 用户还可以使用raise和assert语句引发异常。 引发异常。 异常是具有直接或间接继承自BaseException类的类的对象实例。 错误可能表明一个合理的应用程序不应尝试捕获的严重问题,而异常可能表明一个应用程序应尝试捕获的条件。 错误是未经检查的异常的一种形式,并且是不可恢复的,就像OutOfMemoryError一样,程序员不应该尝试处理。

You can reach me at my LinkedIn link here and on my email: design4led@gmail.com.

你可以在我的LinkedIn链接到我这里design4led@gmail.com:和我的电子邮件。

My Previous Articles:

我以前的文章:

  1. Robotic Vision in Agriculture

    农业机器人视觉

  2. Interesting 10 Machine Learning and Data Science Projects with Datasets

    有趣的10个带有数据集的机器学习和数据科学项目

  3. Basic Understanding of NLP With Python

    使用Python对NLP的基本了解

  4. Zero to Hero in Python from Basic to OOPs Concept

    从Python零到英雄从基本到OOPs概念

翻译自: https://medium.com/towards-artificial-intelligence/exception-handling-in-python-error-type-thing-866539e1ad3f

python错误与异常处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值