ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python 3 学习笔记:异常代码调试

2019-08-17 15:55:21  阅读:223  来源: 互联网

标签:apple Python 笔记 try 苹果 child print 小朋友 调试


什么是异常

程序运行过程中,产生的错误统称为异常(bug)。这些异常有的可能是语法错误,如关键字输入错误、调用错误等,这一类的异常都是显式的,很好发现;还有一种就是隐式的错误,只用在使用时才会被发现,和使用者的操作有关。

下面介绍一下 Python 常见的异常提示:

异常类型.jpg

异常处理语句

try … except …
在使用时,将可能产生异常的代码放在 try 语句中,把处理结果放在 except 语句中,这样,当 try 后面的代码发生错误时就会执行 except 中的代码。如果 try 后的代码没有异常,则不会执行 except 后的代码。

try:
do some things
except exceptionName:
do some things

exceptionName 用于指定可能出现的异常的名称。如果不指定异常的名称,则表示捕获全部可能发生的异常。

示例代码:

def division():
print("====== 开始分苹果 ======")

apple = int(input("一共有几个大苹果:"))
child = int(input("一共有几个小朋友:"))

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个,剩余 {:d} 个。".format(apple, child, result, remain))
else:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个。".format(apple, child, result))

if name == "main":
try:
division()
except ZeroDivisionError:
print("苹果不能被 0 个小朋友平分!!!")

在捕获异常的时候,可以同时捕获多个异常,如:

try:
do some things
except(ValueError, ZeroDivisionError) as e:
do some things

try … except … else
该语句在 except 之后加了一个 else 语句,用于指定当 try 语句没有发现异常时需要执行的代码,如果 try 语句中发现了异常,则不再执行 else 之后的代码。

try:
do some things
except exceptionName:
do some things
else:
do some things

示例代码:

def division():
print("====== 开始分苹果 ======")

apple = int(input("一共有几个大苹果:"))
child = int(input("一共有几个小朋友:"))

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个,剩余 {:d} 个。".format(apple, child, result, remain))
else:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个。".format(apple, child, result))

if name == "main":
try:
division()
except ZeroDivisionError:
print("苹果不能被 0 个小朋友平分!!!")
except ValueError as e:
print("输入错误!!!", e)
else:
print("苹果分配成功。。。")

try … except … finally
无论 try 语句中是否发生异常,都会执行 finally 之后的代码。

try:
do some things
except exceptionName:
do some things
finally:
do some things

示例代码:

def division():
print("====== 开始分苹果 ======")

apple = int(input("一共有几个大苹果:"))
child = int(input("一共有几个小朋友:"))

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个,剩余 {:d} 个。".format(apple, child, result, remain))
else:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个。".format(apple, child, result))

if name == "main":
try:
division()
except ZeroDivisionError:
print("苹果不能被 0 个小朋友平分!!!")
except ValueError as e:
print("输入错误!!!", e)
else:
print("苹果分配成功。。。")
finally:
print("分配了一次苹果。")

raise
如果某个函数可能会产生异常,但是不想在当前函数中处理该异常,则可以使用 raise 语句在函数中抛出异常,

raise [exceptionName[(reason)]]

其中,exceptionName[(reason)] 为可选参数,用于指定抛出的异常名称及异常信息的描述,如果省略则把异常原样抛出。

示例代码:

def division():
print("====== 开始分苹果 ======")

apple = int(input("一共有几个大苹果:"))
child = int(input("一共有几个小朋友:"))

if apple < child:
    raise ValueError("苹果太少了不够分!")

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个,剩余 {:d} 个。".format(apple, child, result, remain))
else:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个。".format(apple, child, result))

if name == "main":
try:
division()
except ZeroDivisionError:
print("苹果不能被 0 个小朋友平分!!!")
except ValueError as e:
print("输入错误!!!", e)
else:
print("苹果分配成功。。。")
finally:
print("分配了一次苹果。")

程序测试

使用 IDE 调试

基本上所有的 IDE 都具有代码调试功能,如 Python 自带的 IDLE 和 PyCharm 等等。一般都是在出现异常的地方设置断点,然后在此处查看数据的值是否正确。具体的内容我也在学习过程中,没有可以调试的代码,以后如果有资料了在进行补充。

使用 assert 语句调试

该语句一般用于对程序在某个时刻必须满足的条件进行验证,

1
assert expression [reason]

其中,expression 是一个条件表达式,如果为假则抛出 AssertError 异常,反之则什么都不做。reason 为可选参数,用于描述前面的 expression 为了更好的知道哪里出现了错误。

示例代码:

def division():
print("====== 开始分苹果 ======")

apple = int(input("一共有几个大苹果:"))
child = int(input("一共有几个小朋友:"))

assert apple > child, "苹果不够分。"

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个,剩余 {:d} 个。".format(apple, child, result, remain))
else:
    print("一共 {:d} 个大苹果平均分给 {:d} 个小朋友,每人 {:d} 个。".format(apple, child, result))

if name == "main":
try:
division()
except ZeroDivisionError:
print("苹果不能被 0 个小朋友平分!!!")
except ValueError as e:
print("输入错误!!!", e)
else:
print("苹果分配成功。。。")
finally:
print("分配了一次苹果。")

标签:apple,Python,笔记,try,苹果,child,print,小朋友,调试
来源: https://blog.51cto.com/14499640/2430345

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有