第9章 异常处理与程序调试

本章知识点:

1、try...except语句;
2、raise语句;
3、assert语句;
4、自定义异常;
5、程序调试;

内容:

9.1 异常的处理

9.1.1 Python中的异常
 
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

 

Python中的异常
1StopItertion当迭代器中没有数据项时触发,由内置函数next()和迭代器的__next__()方法触发。
2ArithmeticError  算法异常的基类,包括OverflowError(溢出异常)、ZeroDivisionError(零除异常)和FloatingPointError(失败的浮点数操作)。
3AssertionErrorassert语句失败时触发。
4AttributeError属性引用和属性赋值异常。

 

 

  
  

 

 

 

 

 

9.1.2 try...except的使用
1 ## finally错误的用法
2 try:
3     f = open("hello.txt", "r")
4     print ("读文件")
5 except FileNotFoundError:
6     print ("文件不存在")
7 finally:
8     f.close()
9 # 输出:读文件
View Code
1 try:
2     open("hello.txt", "r")
3     print ("读文件")
4 except FileNotFoundError:
5     print ("文件不存在")
6 except:
7     print ("程序异常")
8 # 输出:文件不存在
View Code
1 try:
2     result = 10/0
3 except FileNotFoundError:
4     print ("0不能被整除")
5 else:
6     print (result)
7 # 输出:文件不存在
8 # 输出:ZeroDivisionError: division by zero
View Code
 1 try:
 2     s = "hello"
 3     try:
 4         print (s[0] + s[1])
 5         print (s[0] - s[1])
 6     except TypeError:
 7         print ("字符串不支持减法运算")
 8 except:
 9     print ("异常")
10 # 输出:he
11 # 字符串不支持减法运算
View Code
9.1.3 try...finally的使用
1 ## finally错误的用法
2 try:
3     f = open("hello.txt", "r")
4     print ("读文件")
5 except FileNotFoundError:
6     print ("文件不存在")
7 finally:
8     f.close()
9 # 输出:读文件
View Code
 1 try:
 2     f = open("hello.txt", "r")
 3     try:
 4         print (f.read(5))
 5     except:
 6         print ("读取文件错误")
 7     finally:
 8         print ("释放资源")
 9         f.close()
10 except FileNotFoundFrrpr:
11     print ("文件不存在")
12 # 输出:hello
13 # 释放资源
View Code
9.1.4 使用raise抛出异常
1 try:
2     s = None
3     if s is None:
4         print ("s是空对象")
5         raise NameError
6     print (len(s))
7 except TypeError:
8     print ("空对象没有长度")
View Code
9.1.5 自定义异常
 1 from __future__ import division
 2 class DivisionException(Exception):
 3     def __init__(self, x, y):
 4         Exception.__init__(self, x, y)
 5         self.x = x
 6         self.y = y
 7 if __name__ == "__main__":
 8     try:
 9         x = 3
10         y = 2
11         if x % y > 0:
12             print (x / y)
13             raise DivisionException(x, y)
14     except DivisionException,div:
15         print ("DivisionException:x / y = %.2f" % (div.x / div.y))
View Code

 

9.1.6 assert语句的使用
1 t = ("hello",)
2 assert len(t) >= 1
3 t = ("hello")
4 assert len(t) == 1
View Code
1 month = 13
2 assert 1 <= month <= 12, "month errors"
View Code
9.1.7 异常信息
1 import sys
2 try:
3     x = 10 / 0
4 except Exception as ex:
5     print (ex)
6     print (sys.exc_info())
7 # 输出:division by zero
8 # (<class 'ZeroDivisionError'>, ZeroDivisionError('division by zero',), <traceback object at 0x000001E0DA72AD88>)
View Code

9.2 使用自带IDLE调试程序

1 def fun():
2     a = 10
3     b = 0
4     return a / b
5 def format():
6     print ("a / b =" + str(fun()))
View Code

9.3 使用Easy Eclipse for Python调试程序

9.3.1 新建工程
9.3.2 配置调试
9.3.3 设置断点

9.5 习题

1) 选择自己喜欢的工具,练习调试方法。
2) 把前面学习过的例子,都用本章学习的方法增加异常处理。

转载于:https://www.cnblogs.com/wssys/p/10207915.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值