Python错误和Python中的内置异常

A small typing mistake can lead to an error in any programming language because we must follow the syntax rules while coding in any programming language.

一个小的输入错误可能会导致任何一种编程语言出现错误,因为在使用任何一种编程语言进行编码时,我们必须遵循语法规则。

Same is the case with python, in this tutorial we will learn about syntax errors and exceptions in python along with listing down some of the commonly occuring Exceptions in python.

与python相同,在本教程中,我们将了解python中的语法错误和异常,并列出python中一些常见的Exception。

Python:语法错误 (Python: Syntax Error)

This is the most common and basic error situation where you break any syntax rule like if you are working with Python 3.x version and you write the following code for printing any statement,

这是最常见和最基本的错误情况,您会破坏任何语法规则,例如您使用的是Python 3.x版本,并编写以下代码以打印任何语句,

print "I love Python!"

SyntaxError: Missing parentheses in call to 'print'.

SyntaxError:对“ print”的调用中缺少括号。

Because, Python 3 onwards the syntax for using the print statement has changed. Similarly if you forget to add colon(:) at the end of the if condition, you will get a SyntaxError:

因为,从Python 3开始,使用print语句的语法已更改。 同样,如果你忘了加上冒号( : )在年底if条件下,你会得到一个语法错误

if 7 > 5
    print("Yo Yo!")

SyntaxError: invalid syntax

SyntaxError:语法无效

Hence syntax errors are the most basic type of errors that you will encounter while coding in python and these can easily be fixed by seeing the error message and correcting the code as per python syntax.

因此,语法错误是您在python中进行编码时会遇到的最基本的错误类型,可以通过查看错误消息并按照python语法更正代码来轻松解决这些错误。

Python:什么是例外? (Python: What is an Exception?)

Contrary to syntax error, exception is a type of error which is caused as a result of malfunctioning of the code during execution.

与语法错误相反,异常是由于执行期间代码故障导致的一种错误。

Your code might not have any syntax error, still it can lead to exceptions when it is executed.

您的代码可能没有任何语法错误,但在执行时仍可能导致异常。

Let's take the most basic example of dividing a number by zero:

让我们以将数字除以零的最基本示例为例:

a = 10
b = 0
print("Result of Division: " + str(a/b))

Traceback (most recent call last): File "main.py", line 3, in <module> print("Result of Division: " + str(a/b)) ZeroDivisionError: division by zero

追溯(最近一次调用最近):文件“ main.py”,第3行,位于<module> print(“除法结果:” + str(a / b))ZeroDivisionError:被零除

As we can see in the output, we got ZeroDivisionError while the syntax of our python code was absolutely correct, because in this case the error or we should say the exception was generated while code execution.

从输出中可以看到,当Python代码的语法绝对正确时,我们得到ZeroDivisionError ,因为在这种情况下,错误或应该说异常是在代码执行时生成的。

Python returns a very detailed exception message for us to understand the origin point and reason for the exception so that it becomes easier for us to fix our code.

Python返回了非常详细的异常消息 ,以供我们了解异常的起点和原因,以便我们更轻松地修复代码。

解码Python中的异常消息 (Decoding the Exception Message in Python)

What does exception message means in python

The term Traceback in the exception message means that python has traced back the code to the point from where the exception occured and will be showing the related messages after this line.

异常消息中的回溯一词意味着python已将代码追溯到发生异常的位置,并将在此行之后显示相关消息。

The second line in the exception message, as you can see above, tells us the name of the python file and the exact line number for the code due to which exception was generated.

如上所示,异常消息中的第二行告诉我们python文件名称以及由于产生异常而产生的代码的确切行号

If that is still not helpful for someone, in the third line of exception message the complete code statement which lead to the exception is printed.

如果那仍然对某人没有帮助,则在异常消息的第三行中,将打印出导致异常的完整代码语句。

And then in the last line, python tells us which exception/error occured, which in our example above is ZeroDivisionError.

然后在最后一行,python告诉我们发生了哪个异常/错误,在上面的示例中为ZeroDivisionError

内置Python异常类 (In-built Python Exception classes)

Let's learn about a few exception classes along with common reasons for their occurence for our future reference.

让我们了解一些异常类以及它们发生的常见原因,以供将来参考。

NOTE: We are calling them exception class as all these are defined in python as classes.

注意:我们称它们为异常类,因为所有这些都在python中定义为类。

Exception classDescription
AttributeErrorThis exception occurs when the attribute that we are tryinh to access(whether for assigning a value or getting a value) doesn't exists. For example: Trying to access a class member variable which is not defined in class.
ImportErrorThis exception occurs when the imported module is not found.
IndentationErrorThis exception occurs when there is some issue with the code indentation.
TypeErrorWhen an operation is executed on a variable of incorrect type.
ValueErrorWhen for a function, argument value is incorrect.
ZeroDivisionErrorAs discussed above, when we try to divide a number with zero.
TabErrorWhen the indentation is not consistenet throughout the code in terms of tabs and spaces used for indentation.
RuntimeErrorWhen an error is not of any specific defined exception type, python calls it RuntimeError.
NameErrorWhen a variable name we are trying to use is not defined.
异常类 描述
AttributeError 当我们尝试访问的属性(无论是分配值还是获取值)不存在时,就会发生此异常。 例如:尝试访问未在类中定义的类成员变量。
ImportError 当找不到导入的模块时,会发生此异常。
IndentationError 代码缩进出现问题时,将发生此异常。
TypeError 对错误类型的变量执行操作时。
ValueError 对于函数,参数值不正确。
ZeroDivisionError 如上所述,当我们尝试将数字除以零时。
TabError 当缩进不包含缩进时,在整个代码中都使用用于缩进的制表符和空格。
RuntimeError 当错误不是任何特定的已定义异常类型时,python将其称为RuntimeError。
NameError 当我们尝试使用的变量名未定义时。

These were some of the built-in excpetion classes which are most commonly encountered while coding in python. For all the exception types in python check the official documentation of python.

这些是一些内置的exception类,这些类在python编码时最常遇到。 对于python中的所有异常类型,请查看python的官方文档

翻译自: https://www.studytonight.com/python/introduction-to-error-exception-python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值