011: Errors and Exceptions
2016-01-10 00:07
316 查看
1. Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python
2. Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal. And example below would be very mindful:
3. Some objects define standard clean-up actions to be undertaken when the object is no longer needed, regardless of whether or not the operation using the object succeeded or failed.
>>> print(a * 3) Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> print(a * 3) NameError: name 'a' is not defined
2. Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal. And example below would be very mindful:
class MyError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) def learn_raise_error(num): if num == 1: result = num / 0 elif num == 2: result = num * a elif num == 3: result = num + 'learn' elif num == 4: raise MyError(4) elif num == 5: raise Exception('Unkown excetion!') else: num = 6 for i in range(1,7,1): try: learn_raise_error(i) except ZeroDivisionError: print('ZeroDivisionError') except NameError: print('NameError') except TypeError: print('TypeError') except MyError: print('MyError') except: print('Unkown Error') # when no exception, then execute else else: print('No Error') # clean up finally: print('clean up...')
3. Some objects define standard clean-up actions to be undertaken when the object is no longer needed, regardless of whether or not the operation using the object succeeded or failed.
with open("myfile.txt") as f: for line in f: print(line, end="")
相关文章推荐
- JDBC简易入门-各层的写法-02
- mac学习笔记:brew 安装nginx
- UIView的一些知识
- tomcat启动没有8080端口
- Centos7最小化终端命令行安装VMware-tools.pl
- Swift学习笔记1
- Linux Kernel 4.0 RC5 发布!
- Ubuntu Touch 升级即将来袭!
- Linux 下无损图片压缩小工具介绍
- 一张图看尽 Linux 内核运行原理
- Sourcegraph:开发者的福音
- python轻松查到删除自己的微信好友
- 详解Python发送邮件实例
- PHP生成各种常见验证码和Ajax验证过程
- PHP 设计模式系列之 specification规格模式
- 详解YII关联查询
- SQL SERVER使用ODBC 驱动建立的链接服务器调用存储过程时参数不能为NULL值
- Node.js静态文件服务器改进版
- 基于javascript实现彩票随机数生成(简单版)
- [链表]九度OJ 题目1188:约瑟夫环