我是靠谱客的博主 拼搏方盒,最近开发中收集的这篇文章主要介绍python error exception_python中的error和exception基础,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

2ff34e647e2e3cdfd8dca593e17d9b0a.png

Syntax errors, also known as parsing errors.

语法错误,也就是不符合python语法规范,比如:

1

2

3

4

5>>> while True print('Hello world')

File "", line 1

while True print('Hello world')

^

SyntaxError: invalid syntax

这里的while后面少了一个冒号:,解析器会检测到最早的一个错误。

Exceptions

Even if a statement of exception is syntactically correct, It may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal.1

2

3

4

5

6

7

8

9

10

11

12>>> 10 * (1/0)

Traceback (most recent call last):

File "", line 1, in

ZeroDivisionError: division by zero

>>> 4 + spam*3

Traceback (most recent call last):

File "", line 1, in

NameError: name 'spam' is not defined

>>> '2' + 2

Traceback (most recent call last):

File "", line 1, in

TypeError: Can't convert 'int' object to str implicitly

The last line of the error message indicates what happened. Excptions come in different types, and the type is printed as part of the message: The type in the example are ZeroDivisionError, NameError and TypeError.

Handling exceptions

It is possible to write programs that handle selected exceptions. Following example, which asks the user for input until a valid integer has been entered.1

2

3

4

5

6while True:

try:

x = int(input("Please enter a number:"))

break

except ValueError:

print("no valid, try aganin:")

But allows the user to interrupt the program(Using Control-C or whatever the operating system supports); Note that a user-generated interruption is signalled by rasing the KeyboardInterrupt exception.1

2

3

4

5Please enter a number:

Traceback (most recent call last):

File "C:UsersSongChengjunDesktopq.py", line 3, in

x = int(input("Please enter a number:"))

KeyboardInterrupt

The try statement works as follow:First, the try clause(the statement(s)between the try and except keywords)is executed,

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

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

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

A try statement may have more than one except clause. to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may name multiple exceptions as a parenthesized tuple, for example:1

2except(RuntimeError, TypeError, NameError):

pass

最后

以上就是拼搏方盒为你收集整理的python error exception_python中的error和exception基础的全部内容,希望文章能够帮你解决python error exception_python中的error和exception基础所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部