views:

61

answers:

2

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, developers usually apply the suffix 'Exception'. Why is this?

+1  A: 

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.

ThiefMaster
According to that logic, all classes should be pre- or postfixed with the project name and the phase of the moon - it avoids name conflicts, right?
Michael Borgwardt
he +1 for the phase of the moon. I look forward to see InvalidArgumentAnotherShoppingCartFullMoonException
Artefacto
@borgwardt - You're confusing "sometimes is a good idea" with "you always have to".
Syntax Error
+6  A: 

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.

Pekka
I thought this, but it will only ever be used in the context of class FileNotFound extends Exception, or when throwing and catching.
Pheter
@Pheter yes, but if you work with an IDE, the class will appear in your project explorer/code explorer, and may create confusion. With the "Exception" prefix, things are clearer in the long run IMO.
Pekka
Good point, I hadn't considered that. Thanks!
Pheter