Python中的错误和异常处理简单操作示例【try-except用法】
作者:坏蛋是我 时间:2022-09-23 08:00:29
本文实例讲述了Python中的错误和异常处理操作。分享给大家供大家参考,具体如下:
#coding=utf8
print '''''
程序编译时会检测语法错误。
当检测到一个错误,解释器会引发一个异常,并显示异常的详细信息。
在代码中添加错误检测及异常处理,只需要将代码封装在try-except语句中。
try:
try_suite
except :
except_suite
-------------------------------------------------------------------------------
try之后的代码组就是用户管理的代码。
except之后的代码组是处理错误的代码。
'''
try:
filename=raw_input("Enter file name:")
output=open(filename,'r')
for eachline in output:
print eachline
output.close()
except IOError,e:
print "file open error:",e
运行结果:
更多Python相关内容感兴趣的读者可查看本站专题:《Python入门与进阶经典教程》、《Python字符串操作技巧汇总》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
标签:Python,错误,异常处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python+OpenCV内置方法实现行人检测
2023-10-19 12:52:47
![](https://img.aspxhome.com/file/2023/7/81857_0s.jpg)
python版百度语音识别功能
2023-02-28 09:56:34
![](https://img.aspxhome.com/file/2023/3/75303_0s.jpg)
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
2023-11-17 02:40:14
让设计散发文化韵味
2009-03-22 15:01:00
![](https://img.aspxhome.com/file/UploadPic/20093/22/e69687e58c96e6a8a1e59e8b-85s.jpg)
Django ORM 多表查询示例代码
2021-07-25 05:22:02
python 实现 hive中类似 lateral view explode的功能示例
2021-08-20 13:51:45
![](https://img.aspxhome.com/file/2023/6/101236_0s.jpg)
python批量读取txt文件为DataFrame的方法
2021-09-29 12:04:21
![](https://img.aspxhome.com/file/2023/6/77786_0s.jpg)
鼠标驱动图片变化
2009-07-26 10:03:00
php日期转时间戳,指定日期转换成时间戳
2023-06-20 17:02:23
Python实现的数据结构与算法之队列详解
2021-06-06 09:58:05
![](https://img.aspxhome.com/file/2023/2/88872_0s.png)
Python+Pygame实现彩色五子棋游戏
2021-03-29 23:47:34
![](https://img.aspxhome.com/file/2023/6/97686_0s.jpg)
Oracle数据表分区的策略
2010-07-28 12:59:00
去掉CSS赘余代码,CSS可以更简洁
2008-11-05 13:07:00
什么是python类属性
2021-07-31 20:27:16
Access 2003开发者扩展工具集概述
2009-03-19 18:32:00
python如何正确使用yield
2023-09-29 16:57:05
php设置编码格式的方法
2023-11-14 12:01:22
CSS代码实现下划线样式的输入框效果
2010-03-16 12:42:00
![](https://img.aspxhome.com/file/UploadPic/20103/16/201031613047449s.jpg)
自定义asp错误信息的显示
2007-09-13 12:33:00
Python采集图片数据的实现示例
2023-03-06 05:53:17
![](https://img.aspxhome.com/file/2023/5/75075_0s.png)