python中的异常是什么_Python中的异常是什么?

异常是事件,该事件在程序执行期间发生,破坏了程序指令的正常流程。通常,当Python脚本遇到无法解决的情况时,它将引发异常。异常是表示错误的Python对象。

当Python脚本引发异常时,它必须立即处理该异常,否则它将终止并退出。

处理异常

如果您有一些可疑代码可能引发异常,则可以通过将可疑代码放在try:块中来保护程序。在try:块之后,包括一个except:语句,然后是一段代码,该代码块尽可能优雅地处理问题。

语法

这是try .... except ... else块的简单语法-try:

You do your operations here;

......................

except ExceptionI:

If there is ExceptionI, then execute this block.

except ExceptionII:

If there is ExceptionII, then execute this block.

......................

else:

If there is no exception then execute this block.

以下是有关上述语法的一些要点-一个try语句可以有多个except语句。当try块包含可能引发不同类型异常的语句时,这很有用。

您还可以提供一个通用的except子句,该子句可以处理任何异常。

在except子句之后,可以包括else子句。如果try:块中的代码未引发异常,则执行else块中的代码。

else块是不需要try:块保护的代码的好地方。

示例

此示例打开文件,在文件中写入内容,然后正常显示,因为根本没有问题-#!/usr/bin/python

try:

fh = open("testfile", "w")

fh.write("This is my test file for exception handling!!")

except IOError:

print "Error: can\'t find file or read data"

else:

print "Written content in the file successfully"

fh.close()

输出结果

这产生以下结果-Written content in the file successfully

示例

此示例尝试在您没有写许可权的情况下打开文件,因此引发异常-#!/usr/bin/python

try:

fh = open("testfile", "r")

fh.write("This is my test file for exception handling!!")

except IOError:

print "Error: can\'t find file or read data"

else:

print "Written content in the file successfully"

输出结果

这产生以下结果-Error: can't find file or read data

无例外的例外条款

您还可以使用except语句,没有异常定义如下:try:

You do your operations here;

......................

except:

If there is any exception, then execute this block.

......................

else:

If there is no exception then execute this block.

这种try-except语句捕获所有发生的异常。但是,使用这种try-except语句并不是一种好的编程习惯,因为它可以捕获所有异常,但不能使程序员识别可能出现的问题的根本原因。

具有多个例外的例外条款

您还可以使用相同的except语句来处理多个异常,如下所示:try:

You do your operations here;

......................

except(Exception1[, Exception2[,...ExceptionN]]]):

If there is any exception from the given exception list,

then execute this block.

......................

else:

If there is no exception then execute this block.

最后审判条款

您可以将finally:块与try:块一起使用。finally块是放置必须执行的所有代码的位置,无论try块是否引发异常。try-finally语句的语法是:try:

You do your operations here;

......................

Due to any exception, this may be skipped.

finally:

This would always be executed.

......................

您不能同时使用else子句和finally子句。

示例#!/usr/bin/python

try:

fh = open("testfile", "w")

fh.write("This is my test file for exception handling!!")

finally:

print "Error: can\'t find file or read data"

输出结果

如果您无权以写入模式打开文件,则将产生以下结果-Error: can't find file or read data

相同的例子可以更清晰地写成如下-

示例#!/usr/bin/python

try:

fh = open("testfile", "w")

try:

fh.write("This is my test file for exception handling!!")

finally:

print "Going to close the file"

fh.close()

except IOError:

print "Error: can\'t find file or read data"

当try块中引发异常时,执行立即转到finally块。执行完finally块中的所有语句之后,如果在try-except语句的下一个更高层中存在,则再次引发异常,并在except语句中对其进行处理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值