[简明Python教程3.2版本实现] try_except.py
2012-04-11 18:35
639 查看
下午一直受困与简明教程try_except.py中的except ShortInputException , x:的3.2版本的实现方法,
最后得出用except ShortInputException as x:可以实现。
有点恍然大悟的感觉~
额......菜鸟Python初学者伤不起,有木有,有木有!
最后得出用except ShortInputException as x:可以实现。
有点恍然大悟的感觉~
额......菜鸟Python初学者伤不起,有木有,有木有!
#!/usr/bin/python # Filename : try_except.py import sys try: s = input('Enter something -->') except EOFError: print('\nWhy did you do an EOF on me?') sys.exit() # exit the program except: print('\nSome error/exception occurred!') # here, we are not exiting the program print('Done ~ . ~') class ShortInputException(Exception): ''' A user-defined exception class. ''' def __init__(self, length, atleast): Exception.__init__(self) self.length = length self.atleast = atleast try: s = input('Enter somethin -->') if len(s) < 3: raise ShortInputException(len(s), 3) # Other work can continue sa usual here except EOFError: print('\nWhy did you do an EOF on me?') except ShortInputException as x: print('ShortInputException: The input was of length %d,\ was expecting at least %d' %(x.length, x.atleast)) else: print('No exception was raised!')
相关文章推荐
- [简明Python教程3.2版本实现] pickling.py
- 使用Python的web.py框架实现类似Django的ORM查询的教程
- 使用Python的web.py框架实现类似Django的ORM查询的教程
- [概率论与数理统计&Python实现]简明教程
- 简明Python教程命令行地址簿的Python与Java实现
- Python 异常处理 Python 基础教程 try..except
- nginx tomcat集群配置实现无痛重启服务教程python语言版本
- nginx tomcat集群配置实现无痛重启服务教程python语言版本
- Python 异常处理 Python 基础教程 try..except
- Python有道翻译2.1版本爬虫实现
- 简明Python教程学习笔记_6_面向对象编程
- 跳一跳小游戏辅助(手动版本)py代码实现
- Cocos2d-x教程(29)-3.x版本遮罩层实现捕鱼达人滚动数字表盘
- python的try....except、raise的用法
- 简明python教程自学笔记(2)
- python try/except/finally
- python try/except/finally
- 简明python教程 --C++程序员的视角(一):数值类型、字符串、运算符和控制流
- 飘逸的python - 简明gzip模块压缩教程
- 简明python教程学习笔记(二)