views:

68

answers:

4

In the current package that I am working on for a project I have about 13 different custom exceptions. Is it a good idea to place these into their own subpackage below the package that has all the classes that will be using these exceptions?

For example:

com.company.project.core

contains a bunch of classes which can throw some custom exceptions which are then placed inside of

com.company.project.core.exception

The only downside I see right now is that I have few more imports for the classes using the exceptions.

+1  A: 

Yes, an exception package is a useful idea and using any respectable IDE will avoid the problem of having to deal with imports anyway.

A much more difficult question for most programmers is though, whether to use RuntimeExceptions or checked exceptions for their own exception hierarchy.

Christopher Oezbek
A: 

This is a bit of a subjective question but I don't really see the need for a separate package. The custom exceptions are related to the classes within that same package so it is justified to keep them in the same package.

I can't think of any examples of APIs I use where custom exceptions are in their own subpackage. Certainly the JDK doesn't.

Mark
+2  A: 

I don't think exception should go into another package, as they are connected to the other classes in your project. If a package is too crowded with many classes you should split that package, but I would prefer splitting by functional criteria.

Anyways, with so many exception-classes I would consider a base-exception-class, that can be thrown instead of multiple specialized exceptions. Something like MyProjectException.

Mnementh
Yes, a functional separation, not a physical one determined by type. Otherwise you would set the precedent for things like x.y.interfaces and x.y.abstract and other such breakdowns.
Robin
After some back and forth I've decided to refactor some of the specialized exceptions and make the "base-exception-class" that already exists more useful.
Herminator
A: 

Given the number of exceptions you are defining, I think using a subpackage is a good idea. It will keep your core package tidier. From the package structure of core.exceptions it's clear to see what the exception classes relate to so I don't agree with Mnementh's comment.

Steve Claridge