以下语句引用HappyTree(笨笨·天行健)在某个问题中的回复。
贴一段来自BS的TCPL话:
When a program is composed of
separate modules, and especially when those modules come
from separately developed libraries, error handling needs
to be separated into two distinct parts:
[1] The reporting of error conditions that cannot be resolved locally
[2] The handling of errors detected elsewhere
The author of a library can detect run-time errors
but does not in general have any idea what to do
about them. The user of a library may know how to
cope with such errors but cannot detect them -or else
they would be handled in the user's code and not
left for the library to find.
我想这段话还是比较清楚的。当你写一个库时,比如strcpy,你能检测到运行时错误的存在,但你不知道该如何来做一个通用的处理,这时你就可以抛出一个异常,让调用strcpy的用户来处理。
如果你知道如何去处理所有运行时错误,if...else...足够了。