views:

111

answers:

3

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.

+6  A: 

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.

duffymo
Every single one? That just seems like too many small files to keep track of.
Shynthriir
"That just seems like too many small files to keep track of." And that is why you are using Netbeans and not vi for Java development.
Thilo
no, that's why you have directory structures.
sreservoir
@Shynthriir, welcome to Java. ;) But you'll get used to it. With a good IDE it's much quicker to find things and navigate around when the classes are the granular unit.
Kirk Woll
@Thilo ROFL, You hit the nail on the head. I'm way too used to vi for my development work, these fancy IDE's with their buttons and file trees.... /get off my lawn
Shynthriir
I bet I haven't created 3 java exception classes in 3 years. Just what I need, an custom exception that tells me I'm screwed!
Tony Ennis
@Shynt, you might find the IDE large and clunky and perhaps even ugly, but they provide a LOT of useful functionality that vi or other plain editors just don't. For Java an IDE is the right tool.
Thorbjørn Ravn Andersen
Jaysus, how many exception classes are you creating? If you have more than a handful you're probably doing it wrong.
duffymo
Lots of exceptions and vi? WTF are you doing?
duffymo
Bloch Item 60... Favor the use of standard Exceptions. The number of cases in which you need to make up a never-before-seen Exception are probably pretty limited.
RonU
Exactly. There may be a handful of business-domain specific exceptions, but they should have a clear purpose over and above the standard exceptions.
duffymo
+2  A: 

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.

ulty4life
+4  A: 

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.

Thorbjørn Ravn Andersen