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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值