Python Day-例外处理
来源:dev.to
时间:2025-02-05 22:52:33 380浏览 收藏
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《Python Day-例外处理》,文章讲解的知识点主要包括,如果你对文章方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
例外处理
- >例外是一个异常事件,发生在程序执行过程中,并突然停止程序(立即)
>
->异常处理允许响应错误,而不是崩溃运行程序。
语法: try:
# code that might raise an exception
except someexception:
# code to handle the exception
else:
# code to run if no exception occurs
finally:
# code to run regardless of whether an exception occurs
>
1。尝试块try块包含可能引起异常的代码。
- >
- 如果发生异常,则将其传递给块。 2。除了block
block处理在try块中发生的异常。
- >您可以指定不同类型的异常,也可以使用一般条款以捕获所有异常。
4。最后阻止(可选)
- 最终块执行,无论是否出现异常。 >
>示例:
- >
- 1)
-
try: no1 = int(input("enter no.")) no2 = int(input("enter no. ")) print(no1//no2) except zerodivisionerror: print("no2 should not be zero. check no2 value ") print(no1+no2)
>输出:
enter no.10 enter no. 0 no2 should not be zero. check no2 value 102)
try: no1 = int(input("enter no.")) no2 = int(input("enter no. ")) print(no1//no2) print(no1+no2) except zerodivisionerror: print("no2 should not be zero. check no2 value ") except valueerror: print("inputs should be numbers ")
>输出:
enter no.10 enter no. ten inputs should be numbers
3)
try: no1 = int(input("enter no.")) no2 = int(input("enter no. ")) print(no1//no2) print(no1+no2) f = open("pqrs.txt") except zerodivisionerror: print("no2 should not be zero. check no2 value ") except valueerror: print("inputs should be numbers ") except: print("something went wrong")
>输出:
#if all inputs are correct enter no.10 enter no. 5 2 15 #if any error not specified particularly enter no.10 enter no. 5 2 15 something went wrong #if zerodivisionerror enter no.10 enter no. 0 no2 should not be zero. check no2 value #if valueerror enter no.10 enter no. ten inputs should be numbers异常处理和条件语句之间的区别
:
**注意:**
- 处理不可预测的错误时,请使用try-exce
->处理预期条件时使用if-else
追溯模块:
python中的追溯模块用于提取,格式和打印错误跟踪信息,有助于调试和日志记录异常。
>示例:1
import traceback try: no1 = int(input("enter no.")) no2 = int(input("enter no. ")) print(no1//no2) print(no1+no2) f = open("pqrs.txt") print(f.read()) except (valueerror, zerodivisionerror) as msg: print("check ",msg) except: print("something went wrong") traceback.print_exc()
>输出:
enter no.10 enter no. 0 check integer division or modulo by zero>示例:2
import traceback try: no1 = int(input("enter no.")) no2 = int(input("enter no. ")) print(no1//no2) print(no1+no2) f = open("pqrs.txt") print(f.read()) except (valueerror, zerodivisionerror) as msg: print("check ",msg) except: print("something went wrong") traceback.print_exc() finally: print("check finally message")
>输出:
enter no.10 enter no. 10 1 20 something went wrong traceback (most recent call last): file "/home/guru/desktop/guru/python/user.py", line 7, in <module> f = open("pqrs.txt") ^^^^^^^^^^^^^^^^ filenotfounderror: [errno 2] no such file or directory: 'pqrs.txt' check finally message
捕获多个特定异常:
>
->我们可以在单个单个中处理多个异常,除了使用元组。
->使用,因为我们也可以为异常提供一个可变名称。
trackback.print_exc():它提供详细的错误信息(行号,错误类型,消息)。
class employee: pass emp1 = employee() emp2 = employee() print(emp1) print(emp2)
>输出:
<__main__.employee object at 0x730a36434110> <__main__.employee object at 0x730a36434080>
pass关键字的意思是“无所事事”(占位符),因此该类目前为空(它没有属性或方法)。
__ doc __(docstring属性)
> __doc__属性用于访问类,功能,模块或方法的docstring。 docstring是一个多行字符串,提供有关对象的文档,该对象在triple引号('''''')中声明。
>示例:
class employee: '''this class is for creating employees''' print(employee.__doc__)>输出:
4
this class is for creating employees
自我关键字:
->“ self”用于访问和操纵类中的实例变量和方法。 -> self代表类的实例。>
>特定对象的
- 使用self.variable_name定义的>。
->对于每个对象唯一。
>示例:1
class employee: def work(self): print(self.empname, self.job_nature) emp1 = employee() emp1.empname = 'guru' emp1.job_nature = "designing" emp2 = employee() emp2.empname = "pritha" emp2.job_nature = "development" emp1.work() emp2.work()
>输出:
guru designing pritha development
>示例:2类,带3种方法:
class employee: organization = "infosys" def work(self): print(self.empname, self.job_nature, self.organization) def take_leave(self): pass def promote(self): pass emp1 = employee() emp1.empname = 'guru' emp1.job_nature = "designing" emp2 = employee() emp2.empname = "pritha" emp2.job_nature = "development" emp1.work() emp2.work()
>输出:
guru designing infosys pritha development infosys
类特定信息:
类别的信息是指在类的所有实例(对象)之间共享的数据。
>示例:
class employee: def work(self): print(self.empname, self.job_nature, self.organization) def take_leave(self): pass def promote(self): pass emp1 = employee() emp1.empname = 'guru' emp1.job_nature = "designing" emp2 = employee() emp2.empname = "pritha" emp2.job_nature = "development" employee.organization = "infosys" emp1.work() emp2.work()
>输出:
Guru designing Infosys Pritha development Infosys
好了,本文到此结束,带大家了解了《Python Day-例外处理》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
339 收藏
-
184 收藏
-
197 收藏
-
343 收藏
-
324 收藏
-
479 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习