[Python]How to handle the exception in Python?
This post demonstrates how to use try clause to handle the exceptions
- def test_exception(case=None):
- print case
- try:
- if case is None:
- raise ValueError("there is no value")
- elif case == 1:
- raise ImportError("can not import the module")
- else:
- raise MyException("this is the special error")
- except MyException as e:
- print e
- print "this is my exception"
- except ValueError as e:
- print e
- print "this is value error"
- except Exception as e:
- print "this is exception "
- class MyException(Exception):
- pass
- if __name__ == "__main__":
- #test_exception()
- test_exception(1)
[Python]How to handle the exception in Python?的更多相关文章
- 进击的Python【第五章】:Python的高级应用(二)常用模块
Python的高级应用(二)常用模块学习 本章学习要点: Python模块的定义 time &datetime模块 random模块 os模块 sys模块 shutil模块 ConfigPar ...
- 【Python之路】第六篇--Python基础之模块
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- Python接口测试实战3(上)- Python操作数据库
如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...
- 孤荷凌寒自学python第八十天开始写Python的第一个爬虫10
孤荷凌寒自学python第八十天开始写Python的第一个爬虫10 (完整学习过程屏幕记录视频地址在文末) 原计划今天应当可以解决读取所有页的目录并转而取出所有新闻的功能,不过由于学习时间不够,只是进 ...
- 孤荷凌寒自学python第七十九天开始写Python的第一个爬虫9并使用pydocx模块将结果写入word文档
孤荷凌寒自学python第七十九天开始写Python的第一个爬虫9 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 到今天终于完成了对docx模块针对 ...
- 孤荷凌寒自学python第七十三天开始写Python的第一个爬虫3
孤荷凌寒自学python第七十三天开始写Python的第一个爬虫3 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...
- Django,Flask,Tornado三大框架对比,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Django 与 Tornado 各自的优缺点Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使 ...
- python 多线程两种实现方式,Python多线程下的_strptime问题,
python 多线程两种实现方式 原创 Linux操作系统 作者:杨奇龙 时间:2014-06-08 20:24:26 44021 0 目前python 提供了几种多线程实现方式 thread,t ...
- Python小白的发展之路之Python基础(一)
Python基础部分1: 1.Python简介 2.Python 2 or 3,两者的主要区别 3.Python解释器 4.安装Python 5.第一个Python程序 Hello World 6.P ...
随机推荐
- 鸟哥之安裝 CentOS7.x
http://linux.vbird.org/linux_basic/0157installcentos7.php since 2002/01/01 新手建議 開始閱讀之前 網站導覽 Linux 基礎 ...
- ThinkPHP连接数据库出现的错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
最近看了看ThinkPHP.在连接mysql数据库时出现了错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'.意思就是没有PDO(PHP数据对象 ...
- java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)
1.Excutor 源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes th ...
- A Game of Thrones(16) - Edard
“They’ve found her, my lord.” Ned rose quickly. “Our men or Lannister’s?” “It was Jory,” his steward ...
- 在Activity中为什么要用managedQuery()
刚開始接触android的时候,每次用数据库都会犹豫使用哪种方式,一种是getContentResolver().query(...),还有一种是managedQuery(...),后来习惯了使用前一 ...
- UVA 11237 - Halloween treats(鸽笼原理)
11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...
- POJ 1088 滑雪 记忆化优化题解
本题有人写是DP,只是和DP还是有点区别的,应该主要是记忆化 Momoization 算法. 思路就是递归,然后在递归的过程把计算的结果记录起来,以便后面使用. 非常经典的搜索题目,这样的方法非常多题 ...
- xcode target
A target specifies a product to build and contains the instructions for building the product from a ...
- ServiceCallSite
ServiceCallSite 通过上一篇的介绍我们应该对实现在ServiceProvider的总体设计有了一个大致的了解,但是我们刻意回避一个重要的话题,即服务实例最终究竟是采用何种方式提供出来的. ...
- H3C TE老版本OSPF正确配置
R1配置: ---------------------------------------------------- # sysname RT1# super password level 3 cip ...