Reputation: 14032
Name of Exception class in Java must have Exception
suffix also describe its throwing situation. Now I have two exception classes in about primary external storage in Android
:
/**
* Thrown when Application tries to access primary external storage and it is not
* available for write.This only depends on status of storage, for example media
* not mounted,bad mounted, or ... .
*/
public class PrimaryExternalStorageIsNotReadyToWriteException extends Exception {
...
}
/**
* Thrown when Application tries to write on a directory
* of primary external storage that needs
* {@link Manifest.permission#WRITE_EXTERNAL_STORAGE} permission but that
* permission has not granted to the Application.
*/
public class WriteToPrimaryExternalStoragePermisionException extends RuntimeException {
...
}
As you see, names are long, but I can not remove Exception
or PrimaryExternalStorage
from names. Also I do not want to use SecurityException
or other existing exceptions because those are general. I know long names are not forbidden but using and reminding them is hard. The only thing I can think is creating a package with name primaryexternalstorageexceptions
and change names to IsNotReadyToWriteException
and WritePermisionException
. But is it a good way? And is there a better way to avoid those long names?
Upvotes: 1
Views: 187
Reputation: 100289
If you are using PrimaryExternalStorage
pretty often in your program, it seems ok to introduce (and document) an abbreviation like PES
and use PESIsNotReadyToWriteException
, WriteToPESPermissionException
(or PesIsNotReadyToWriteException
, WriteToPesPermissionException
depending on your policy of using abbreviations in camelCased identifiers).
Note that Is
in your first exception is definitely redundant. See, for example, JDK exceptions like ArrayIndexOutOfBoundsException
is not ArrayIndexIsOutOfBoundsException
.
Another thing which comes in mind is to make your exceptions somewhat more general like PrimaryExternalStorageNotReadyException
(not ready for anything, not just write) and PrimaryExternalStoragePermissionException
(actual missing permission were it write or read may be passed as exception parameter).
Upvotes: 2
Reputation: 707
Creating a separate package primaryexternalstorageexceptions
for a single exception class is killing the concept of Packages in java. If you feel that there would be more than 5 or 6 Exception class that would be part of primaryexternalstorageexceptions
, then you can got for it. I personally do not think to have a separate package.
I would suggest you to shorten the class name likewise:
PrimaryExternalStorageIsNotReadyToWriteException
to PrmyExtlStrgIsNotReadyToWriteException
The standard is to what you have now, which is a long class name. Anyhow, the Exceptions are packaged and it will not be hard for you to go to a package and search for the class.
Upvotes: -1