Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 499
Peter Horne Avatar asked May 18 '10 21:05

Peter Horne


People also ask

How do you name an exception class?

That a class represents an exception should also be obvious either by its parentage or by its name. The name should represent whatever the problem is, and should do so directly and specifically. To add Exception to the end is either redundant — so remove it — or an indication of a poor name — so rename it.

What is the exception class?

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions.

Why is understanding exception hierarchy important?

Exceptions Hierarchy in Java The Exception class presents all the Exceptions that you might need to handle while working with the Java programming language. Some commonly known and encountered examples of such Exceptions include NullPointerException, ClassNotFoundException, IllegalArgumentException, etc.


2 Answers

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.

like image 60
Pekka Avatar answered Oct 14 '22 07:10

Pekka


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.

like image 40
ThiefMaster Avatar answered Oct 14 '22 05:10

ThiefMaster