首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python 错误处理

程序在运行的过程中总是会遇到各种各样的问题,有一部分是 BUG,另外一部分我们称之为异常(或错误)。大多数编程语言均使用以下语句来处理异常,Python 也不例外。

try:

pass

except:

pass

finally:

pass

先来看一个最简单的处理异常的示例

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

try:

r=1/

except:

print('except')

以上代码执行结果如下

except

当我们认为一段代码可能会出现错误时,我们可以使用 try 语句对错误进行处理,否则错误将一级级向上报,直到有函数可以处理该错误。若无函数处理该错误,程序将推出执行。

在出现错误时我们可以针对错误类型的不同,来输出不同的结果

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

try:

r=1/

exceptZeroDivisionError:

print("The second number can't be zero!")

exceptValueError:

print"Value Error."

执行以上代码,我们将得到以下结果

The second number can't be zero!

现在我们将代码进行修改,修改后结果如下

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

try:

r=1/'1'

exceptZeroDivisionError:

print("The second number can't be zero!")

exceptValueError:

print"Value Error."

执行以上代码,我们将得到以下结果

Value Error.

从以上代码可以看出,针对不同的错误类型我们可以进行不同的输出结果,在 Python 中常用的错误类型如下

在 try 语句中我们可以使用 else 和 finally 关键字,当执行 try 后的内容 except 后的内容被跳过时执行 else 后的内容;而 finally 后的语句无论前面执行的是 try 后的语句还是 except 后的语句都会被执行。

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

whileTrue:

try:

x=raw_input("the first number:")

y=raw_input("the second number:")

r=float(x)/float(y)

except:

print('except try again')

else:

print('else')

finally:

print('finally')

执行结果如下

the first number:1

the second number:2

else

finally

the first number:1

the second number:0

except try again

finally

try...except...在某些情况下能够替代 if...else.. 的条件语句

大多数情况下 python 解释器已经给出了完善的错误提示信息,我们无需在单独编写提示信息,那我们我们该如何使用系统默认的提示信息呢,我们可以通过参数e来获取系统默认的提示信息。

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

whileTrue:

try:

x=raw_input("the first number:")

y=raw_input("the second number:")

r=float(x)/float(y)

exceptZeroDivisionErrorase:

print(e)

else:

print('else')

finally:

print('finally')

执行结果如下

the first number:1

the second number:0

float division by zero

finally

the first number:1

the second number:-

could not convert string to float: '-'

finally

the first number:1

the second number:1

else

finally

在以上代码中我们并未编写任何的错误提示信息,但是在出现错误时程序正常打印了错误信息'float division by zero''could not convert string to float: '-''

程序在运行的过程中总是会遇到各种各样的问题,有一部分是 BUG,另外一部分我们称之为异常(或错误)。大多数编程语言均使用以下语句来处理异常,Python 也不例外。

try:

pass

except:

pass

finally:

pass

先来看一个最简单的处理异常的示例

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

try:

r=1/

except:

print('except')

以上代码执行结果如下

except

当我们认为一段代码可能会出现错误时,我们可以使用 try 语句对错误进行处理,否则错误将一级级向上报,直到有函数可以处理该错误。若无函数处理该错误,程序将推出执行。

在出现错误时我们可以针对错误类型的不同,来输出不同的结果

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

try:

r=1/

exceptZeroDivisionError:

print("The second number can't be zero!")

exceptValueError:

print"Value Error."

执行以上代码,我们将得到以下结果

The second number can't be zero!

现在我们将代码进行修改,修改后结果如下

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

try:

r=1/'1'

exceptZeroDivisionError:

print("The second number can't be zero!")

exceptValueError:

print"Value Error."

执行以上代码,我们将得到以下结果

Value Error.

从以上代码可以看出,针对不同的错误类型我们可以进行不同的输出结果,在 Python 中常用的错误类型如下

在 try 语句中我们可以使用 else 和 finally 关键字,当执行 try 后的内容 except 后的内容被跳过时执行 else 后的内容;而 finally 后的语句无论前面执行的是 try 后的语句还是 except 后的语句都会被执行。

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

whileTrue:

try:

x=raw_input("the first number:")

y=raw_input("the second number:")

r=float(x)/float(y)

except:

print('except try again')

else:

print('else')

finally:

print('finally')

执行结果如下

the first number:1

the second number:2

else

finally

the first number:1

the second number:0

except try again

finally

try...except...在某些情况下能够替代 if...else.. 的条件语句

大多数情况下 python 解释器已经给出了完善的错误提示信息,我们无需在单独编写提示信息,那我们我们该如何使用系统默认的提示信息呢,我们可以通过参数e来获取系统默认的提示信息。

#!/usr/bin/evn python3

# -*- coding:utf-8 -*-

whileTrue:

try:

x=raw_input("the first number:")

y=raw_input("the second number:")

r=float(x)/float(y)

exceptZeroDivisionErrorase:

print(e)

else:

print('else')

finally:

print('finally')

执行结果如下

the first number:1

the second number:0

float division by zero

finally

the first number:1

the second number:-

could not convert string to float: '-'

finally

the first number:1

the second number:1

else

finally

在以上代码中我们并未编写任何的错误提示信息,但是在出现错误时程序正常打印了错误信息'float division by zero''could not convert string to float: '-''

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190109G068UN00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券