2015-06-19 62 views
1

Java中Exception类的名称必须有Exception后缀也描述它的抛出情况。现在我有大约主要外部存储两个异常类在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 { 
... 
} 

正如你看到的,名字很长,但我无法从名称中删除ExceptionPrimaryExternalStorage。此外,我不想使用SecurityException或其他现有的例外,因为这些例外是普遍的。我知道长名不被禁止,但使用和提醒他们很难。我唯一能想到的是创建名称为primaryexternalstorageexceptions的包,并将名称更改为IsNotReadyToWriteExceptionWritePermisionException。但这是一个好方法吗?有没有更好的方法来避免这些长名字?

+3

我看到这些名字没有错。 –

+0

@jangroth感谢您的关注,我真的曾想过关于超类和子类,但不幸的是,一个扩展了'Exception',另一个扩展了'RuntimeException'.So我不能用这种方式。 – hasanghaforian

回答

2

如果你在你的程序中使用PrimaryExternalStorage漂亮的时候,它似乎确定引进(和文件)的缩写像PES和使用PESIsNotReadyToWriteExceptionWriteToPESPermissionException(或PesIsNotReadyToWriteExceptionWriteToPesPermissionException取决于你在驼峰格式标识符使用缩写的政策)。

请注意,Is在您的第一个例外是绝对多余的。例如,请参阅像ArrayIndexOutOfBoundsException这样的JDK异常不是ArrayIndexIsOutOfBoundsException

想到的另一件事是让你的例外更通用一些,如PrimaryExternalStorageNotReadyException(没有准备好任何东西,不只是写)和PrimaryExternalStoragePermissionException(实际上丢失的权限是它的写或读可能作为异常参数传递)。

+0

感谢您的关注。关于您的第一个建议,我不确定,因为文档说:“类名应该是名词,......使用整个词 - 避免首字母缩写词和缩写(除非缩写比长形式,如URL或HTML)。“ – hasanghaforian

-1

为单个异常类创建单独的包primaryexternalstorageexceptions正在破坏java中Packages的概念。如果您认为将会有超过5或6个Exception类别,它将成为primaryexternalstorageexceptions的一部分,那么您可以为此付出代价。我个人认为没有单独的软件包。

我建议你缩短类名同样: PrimaryExternalStorageIsNotReadyToWriteExceptionPrmyExtlStrgIsNotReadyToWriteException

的标准是你所拥有的现在,这是一个很长的类名。无论如何,这些例外都是打包的,并且你不需要去包裹和搜索课程。

+2

这不是一个答案,这是一个意见。而国际海事组织,你提出的“缩略语”并没有真正实现任何显着的缩小规模,减少可读性,并使其更难以记住,正如海报所要求的。 – 323go