Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Java exceptions deserve their own file?

Whenever I define a new exception, I tend to do one of two things:

  1. Create an exceptions file which contains a listing of all the new exceptions defined in my project.
  2. Put each defined exception in the same file as the class in which it is thrown.

Netbeans doesn't doesn't like it when I have a public exception defined in the same file as another class such as in point 2. While I haven't tried it yet, I believe it would yell at me for similar reasons for having more than one exception in the same file as in point 1.

Should each exception really have its own file, or is there some other style which Netbeans will accept? I'm fairly new to Java and Netbeans and would love to hear what the appropriate style is.

like image 805
Anthony Avatar asked Oct 09 '10 01:10

Anthony


People also ask

Can you create your own exceptions in Java?

In Java, we can create our own exceptions that are derived classes of the Exception class. Creating our own Exception is known as custom exception or user-defined exception. Basically, Java custom exceptions are used to customize the exception according to user need.

Do we need to import exception in Java?

Classes from package java. lang. need not be imported. It's not just RuntimeException, but also stuff like Object and String.

Why would you need to write your own exception?

Custom exceptions provide you the flexibility to add attributes and methods that are not part of a standard Java exception. These can store additional information, like an application-specific error code, or provide utility methods that can be used to handle or present the exception to a user.


3 Answers

I put exceptions in their own file. Why not?

You can't have more than one public class in a single file. If you're trying to put a public exception in with another public class in the name of some sort of efficiency, I'd recommend going back and re-reading the Java spec.

like image 198
duffymo Avatar answered Oct 03 '22 07:10

duffymo


An exception is a class and require same treatment as all other classes.

Hence if you need an exception to be public (which is somewhat necessary if you want to actually use it outside a single class) you must put it in its own file. Just like any other class.

No magic, that's just Java has been designed.

You will grow to like the fact that Java has made some very strong design choices. It makes your life easier in the long run.

like image 32
Thorbjørn Ravn Andersen Avatar answered Oct 03 '22 08:10

Thorbjørn Ravn Andersen


It's a good practice to make exceptions separate classes (not inner classes). This makes them more reusable-friendly, so that class A isn't using a class nested within class B. As your class library grows larger, you'll find not having to look for an inner class to be more friendly to development.

Following from this is the best practice of only one class per file, with the file name representing the class name.

That being said, I tend to make exceptions inner classes of the class which throws them unless they are generic enough to be used by other classes. Usually because of development timelines and the fact that I know the exception class is specialized enough to not be used elsewhere. I'm slowly moving toward putting them into separate files as a practice when deadlines are reasonable, because it is more tedious to access an inner class.

like image 37
ulty4life Avatar answered Oct 03 '22 07:10

ulty4life