当前位置:网站首页>学会这招再也不怕手误让代码崩掉
学会这招再也不怕手误让代码崩掉
2022-07-28 00:50:00 【51CTO】
导读
今天学了一招很有意思的,乘热打铁分享一波!
更详细的说是关于python的异常处理的应用,实现一个手误输错也能被原谅的功能,非常好用和有趣哦 >_<

一、异常和异常处理
为了能够对接下来的要完成的事情有个更清晰的认识,我们需要先了解几个概念。
第一,什么是异常。异常就是一个事件,该事件在程序执行过程中发生,影响了程序的正常执行。
打个不恰当的比喻就是,当你在泡妞的时候,突然有个傻雕过来说你有口臭,老是放臭屁,直接让你无法正常泡妞。
第二,什么异常处理,为了解决当出现异常时,我们不希望我们的程序挂掉,而进行的一些操作。
而异常处理就是类似于在你知道这个倒霉蛋来的时候,肯定没有好话,你提前预知到要发生,反手就是给他一巴掌,然后让他好好说话,最后事情就朝着好的方向发展了。
二、异常处理的小应用
我们经常会遇到比如需要输入密码,当你输错密码的时候,你不希望退出这个系统,而是重新输入密码;又或者是要读取一张图片,但是有时候手误打错路径,你不想让整个代码重头再运行一次,为了让代码能够识别你输错并允许你重新输入,直到你最终正确输入路径。
这种情况下,地址输错,代码直接停掉,并不是我们想要的
那我们如何解决呢?
思路:
(1)找到可能出错的地方,进行检测判断;
(2)当输入正确时,直接执行下一步操作;
(3)当输入错误时,重新执行,直到正确。
完整代码: https://github.com/DWCTOD/AI_study/blob/master/06-yichangchuli
三、效果展示
使用系统自带的录屏软件操作一遍吧,让大家看的更加清晰:

难受啊,不知道为什么显示的图片有问题,这个录屏软件有毒,自己电脑上面看的没事的!!!以后再研究一哈。
这里只是简单的演示了一个有趣的功能,对于异常处理,自己也学的不够深,以后有机会的话,继续研究一哈!


边栏推荐
- Zkrollup learning materials summary
- Small bulk quantitative stock trading record | data is the source in the quantitative system, which teaches you to build a universal data source framework
- Principle and implementation of focal loss
- Uniapp summary (applet)
- Principle and implementation of cross entropy
- 【愚公系列】2022年07月 Go教学课程 019-循环结构之for
- Use of recursion: 1. Convert the tiled array to a tree 2. Convert the tree to a tiled array
- Go learn 02 basic knowledge
- Solution of digital commerce cloud supply chain centralized purchase management system: centralized purchase system management mode, digital control of enterprise materials
- 云原生爱好者周刊:Prometheus 架构演进之路
猜你喜欢

每条你收藏的资讯背后,都离不开TA

Codeworks round 810 (Div. 2) a~c problem solution

Redis design specification

Codeforces Round #810 (Div. 2)A~C题解

Data security and privacy computing summit - provable security: Learning

Promise from introduction to mastery (Chapter 2 understanding and use of promise)

如何评估研发人员效能?软件工程师报告帮你看见每个人的贡献

Fluorite network, difficult to be a "lone brave"

54: Chapter 5: develop admin management services: 7: face warehousing process; Face login process; The browser turns on the video debugging mode (so that the camera can also be turned on in the case o

Appium click operation sorting
随机推荐
执行 Add-Migration 迁移时报 Build failed.
Behind every piece of information you collect, you can't live without TA
A letter to the user of qubu drawing bed
Cloud native enthusiast weekly: the evolution of Prometheus architecture
Small bulk quantitative stock trading record | data is the source in the quantitative system, which teaches you to build a universal data source framework
C# 使用Abp仓储访问数据库时报错记录集
[database data recovery] data recovery case of insufficient disk space of SQL Server database
Product interpretation - Design and distributed expansion of metersphere UI test module
Promise from introduction to mastery (Chapter 1 Introduction and basic use of promise)
考研数学一元微分学证明题常见题型方法
go 学习01
小米网站主页面大模块——小模块+导航(浮动案例)
Go learn 02 basic knowledge
Embedded classic communication protocol
uniapp 总结篇 (小程序)
Hcip 13th day notes
zkrollup学习资料汇总
Huawei app UI automation test post interview real questions, real interview experience.
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
Packet capturing wizard netcapture app packet capturing tutorial "complete"