Java 具有长名称的类
Java中异常类的名称必须具有Java 具有长名称的类,java,android,exception,naming-conventions,Java,Android,Exception,Naming Conventions,Java中异常类的名称必须具有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,b
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 {
...
}
如您所见,名称很长,但我无法从名称中删除
Exception
或PrimaryExternalStorage
。另外,我不想使用SecurityException
或其他现有的异常,因为它们是通用的。我知道长名字是不被禁止的,但是使用和提醒它们是很困难的。我唯一能想到的就是创建一个名为PrimaryExternalStorageException
的包,并将名称更改为IsNotReadyToWriteException
和WritePermissionException
。但这是一个好办法吗?有没有更好的方法来避免这些长名称呢?为单个异常类创建一个单独的包primaryexternalstorageexceptions
,这会扼杀java中包的概念。如果您觉得作为primaryexternalstorageexceptions
的一部分的异常类将超过5个或6个,那么您可以得到它。我个人认为没有一个单独的方案
我建议您同样缩短类名:
PrimaryExternalStorageIsNotReadyToWriteException
toPrmyExtLSTRGISNoteryToWriteException
标准是您现在拥有的,这是一个很长的类名。无论如何,异常都是打包的,您不难找到一个包并搜索类 如果您在程序中经常使用
PrimaryExternalStorage
,那么引入(并记录)一个类似PES
的缩写,并使用pesisnotreadytowriteeexception
,writetopespmissionexception
(或pesNotReadyToWriteException
,WriteToPesPermissionException
,具体取决于您在camelCased标识符中使用缩写的策略)
请注意,在第一个异常中,是
绝对是多余的。例如,请参阅类似于ArrayIndexOutOfBoundsException
的JDK异常不是ArrayIndexIsOutOfBoundsException
需要考虑的另一件事是使您的异常更加通用,如
PrimaryExternalStorageNotReadyException
(不准备任何操作,而不仅仅是写入)和PrimaryExternalStoragePermissionException
(实际缺少的写入或读取权限可以作为异常参数传递).我看这些名字没有错。@jangroth谢谢你的关注,我确实考虑过超类和子类,但不幸的是,一个扩展了异常
,另一个扩展了运行时异常
。所以我不能用这种方式。这不是答案,这是一种观点。我是你建议的“缩写”正如海报所要求的那样,不要真的大幅缩小尺寸,降低可读性,让它更难记住。感谢您的关注。关于您的第一个建议,我不确定,因为文档中说:“类名应该是名词,……使用全称避免首字母缩写和缩写(除非缩写比长格式(如URL或HTML)更广泛地使用)