什么是异常,其实我不是很清楚
起因
平时能较好的处理异常。但在自定义异常时才发现,并不了解异常。对于用的api也不真明白为什么要抛出异常,而不用其它方式。
针对自己的疑惑,觉得有必要好好思考下
异常机制的产生
语言发展是从汇编到C再到C++. 在汇编和C语言中都没有异常机制。
它们用的是错误编码机制. 直到C++才出现异常机制。
什么导致C++中出现异常机制?要回答它,需先了解错误编码机制的存在的缺点。
* 错误信息不全
调用信息不全; 错误种类很多,返回会过于简单.
* 默认可以被忽略
默认可以不用检查返回的错误编码,很容易使程序处在不正常状态.
* 不能跨作用域传送
只能一层层的向上传送,每层都有一堆检查错误的代码,会掩盖正常处理。
编码机制是为解决程序错误这个需求而生。在错误发生时最重要的是:
错误内容是什么?
在哪儿出错了?
为什么出错?
编码机制存在着缺点,不能很好解决这几个需求。
异常机制能完善的解决了这些问题. 所以后来语言中都加入了异常机制。
异常机制也就了两个重要特点:
* 沿着调用堆栈向上传递错误
* 不可忽略
前者可获得完整的调用信息,方便定位错误地点. 也可将错误处理代码分离出来。
后者明确拒绝不健康运行状态,是程序正确性的关键。
对异常的疑惑
一个简单的python异常
>>> a = {"a": 1, "b": 2} >>> a.pop(c) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'c' is not defined
对于它我有很多疑问:
1. 为什么抛异常?返回特殊标记不行吗.
2. 什么是异常?为什么它会被定义为异常.
回答第一个问题
异常的特点是就是"不可忽略",上面的pop没有任何值适合作为标记,它只能抛异常. 如果忽略了,可能会让程序处在不健康状态。
回答第二个问题
异常是判断词,与它对的应是正常。
那么什么是正常?回答它也就可回答第二个问题了.
异常与正常其实隐藏着一个判断, 隐藏着一个判断标准。
那么谁在判断,谁在制定标准。答案是你。
你在决定什么是异常,什么时候该抛异常,抛什么类型的异常。
例子中就是python作者在背后做了思考/定义,决定它应该作为异常。
如何使用异常
当定义好什么是正常,知道哪些是不正常而且不可忽略时,就是使用异常的时候。为防止不健康运行状态. 而且不可忽略.
值得注意的是:通常是没法考虑全面的,会有漏掉的情况,应以保守的方法处理,对未考虑到的情况全部作为异常处理,再分辨,逐步完善。
异常的重要性
它是软件好坏的重要标志,任何在这上面处理不好的软件,无不是充满了坑。
2014年1月17日 23:25
异常机制的主要目的是,让程序员专注于happy path,即程序的正常功能,而将错误处理单独编码。这样,程序员在打开一个文件后不需要立即使用if来检测文件是否存在,而是在catch代码块说明当文件不存在时程序应该如何反应,将正常执行路径和异常执行路径分开,使得代码的维护更加简单。
2014年1月18日 13:39
确实是它的一个目的,用的好代码会比较清晰。实际很多人会忽略它,将编码机制与异常机制混用,会让维护的人很痛苦。@sl:
2020年12月14日 15:09
Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!!
2021年2月28日 01:27
I was exactly searching for. Thanks for such post and please keep it up. Great work
2021年7月14日 20:18
Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. CFAH
2022年4月27日 08:34
It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing. สล็อต888
2022年8月08日 21:22
There are certainly quite a few nasty sites that you need to avoid. A good rule of thumb is that the less known a site is, the more likely it is to be a scam. dark web links
2022年8月08日 22:04
You might be surprised to learn that the majority of the people that use these search engines never come across any information about what they are looking for. deep web
2022年8月08日 22:20
You have probably been wondering why you haven't been seeing any results from the links that you've been looking at. It's because the links aren't actually there. dark web links
2022年8月08日 22:35
But how do you find the best service out there? Well, first you need to do your research! Look for services that offer free trials and money back guarantees. dark web sites
2022年8月08日 22:49
However, they may vary in terms of the time required and the complexity involved. So how do you find out how I can make money on the Dark Web? You will have to spend some time browsing through some of the popular search engines. dark web
2022年8月08日 23:07
That is why the Clickbank affiliate marketing secrets that have made so many people so successful are so simple to follow and understand. It just takes a little work on your part to get the ball rolling. work from home jobs
2022年8月08日 23:20
I am still learning and trying my hand at new things as a home-business success story. Every day I learn something new about marketing. affiliate marketing success
2023年1月14日 17:37
Aw, i thought this was quite a good post. In concept I would like to devote writing such as this moreover – spending time and actual effort to produce a great article… but exactly what do I say… I procrastinate alot by no means manage to get something done. 온라인카지노
2023年2月01日 14:33
It is not my first time to pay a quick visit this web page, i am visiting this website dailly and get fastidious information from here daily.
온라인 카지노
카지노사이트
바카라사이트
토토사이트
호텔 카지노
https://www.j9korea.com/
2023年2月12日 22:35
Thanks for the great post on your blog, it really gives me an insight on this topic. 여우알바
============================
It’s rare to get an expert in whom you may have some confidence. In the world of today, nobody actually cares about showing others the way in this matter. How lucky I am to have now found a wonderful web site as this. It is really people like you who make a genuine difference nowadays through the ideas they discuss. Macbook Pro
2023年2月22日 21:09
dress up is always a great and exciting one, or strip poker. Perhaps get some dice from a game board and decide if you land on certain numbers what those actions are. Spice it up is always a great thing!! 여우알바