开发者

Why do exceptions usually have the suffix 'Exception' in the class name?

开发者 https://www.devze.com 2022-12-31 11:40 出处:网络
It seems to me that it is rendundant information as the usage of the class will make it apparent that it is an exception. Furthermore, in PHP, the class must extend the Exception class so it will be a

It seems to me that it is rendundant information as the usage of the class will make it apparent that it is an exception. Furthermore, in PHP, the class must extend the Exception class so it will be apparent that it is an exception when looking at the class on its own.

Despite this, devel开发者_如何学运维opers usually apply the suffix 'Exception'. Why is this?


I know of no binding rule for this, but I guess it simply makes sense.

class FileNotFound

could mean a number of things, while

class FileNotFoundException

makes the Exception character very clear.


Imagine two classes, InvalidIndex and InvalidIndexException. A class named InvalidIndex might be completely valid (e.g. to invalidate database indexes or something like that) and not related to an exception. If you now had an exception for invalid array indices you'd have a naming problem. By suffixing exception classes with Exception, you avoid name conflicts like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消