Class 名字很长
Class with long name
Name of Exception class in Java 必须有 Exception
后缀也描述了它的抛出情况。现在我在 Android
:
中关于主外部存储有两个异常 classes
/**
* 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
或其他现有的异常,因为它们是通用的。我知道不禁止使用长名称,但很难使用和提醒它们。我唯一能想到的就是创建一个名为 primaryexternalstorageexceptions
的包并将名称更改为 IsNotReadyToWriteException
和 WritePermisionException
。但这是一个好方法吗?有没有更好的方法来避免这些长名称?
为单个异常 class 创建单独的包 primaryexternalstorageexceptions
正在扼杀 java 中包的概念。如果你觉得 primaryexternalstorageexceptions
中有超过 5 或 6 个 Exception class,那么你可以得到它。我个人认为没有单独的包。
我建议您同样缩短 class 名称:
PrimaryExternalStorageIsNotReadyToWriteException
到 PrmyExtlStrgIsNotReadyToWriteException
标准是您现在拥有的,这是一个很长的 class 名称。总之,Exceptions是打包好的,你去打包搜索class也不难。
如果您在程序中经常使用 PrimaryExternalStorage
,引入(并记录)PES
等缩写并使用 PESIsNotReadyToWriteException
、WriteToPESPermissionException
似乎没问题(或 PesIsNotReadyToWriteException
、WriteToPesPermissionException
,具体取决于您在驼峰标识符中使用缩写的政策)。
请注意,第一个例外中的 Is
绝对是多余的。例如,参见 JDK 异常,例如 ArrayIndexOutOfBoundsException
不是 ArrayIndexIsOutOfBoundsException
。
想到的另一件事是使您的异常更通用一些,例如 PrimaryExternalStorageNotReadyException
(没有准备好任何事情,不仅仅是写)和 PrimaryExternalStoragePermissionException
(实际缺少的权限是写还是读可以作为异常参数传递)。
Name of Exception class in 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,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
或其他现有的异常,因为它们是通用的。我知道不禁止使用长名称,但很难使用和提醒它们。我唯一能想到的就是创建一个名为 primaryexternalstorageexceptions
的包并将名称更改为 IsNotReadyToWriteException
和 WritePermisionException
。但这是一个好方法吗?有没有更好的方法来避免这些长名称?
为单个异常 class 创建单独的包 primaryexternalstorageexceptions
正在扼杀 java 中包的概念。如果你觉得 primaryexternalstorageexceptions
中有超过 5 或 6 个 Exception class,那么你可以得到它。我个人认为没有单独的包。
我建议您同样缩短 class 名称:
PrimaryExternalStorageIsNotReadyToWriteException
到 PrmyExtlStrgIsNotReadyToWriteException
标准是您现在拥有的,这是一个很长的 class 名称。总之,Exceptions是打包好的,你去打包搜索class也不难。
如果您在程序中经常使用 PrimaryExternalStorage
,引入(并记录)PES
等缩写并使用 PESIsNotReadyToWriteException
、WriteToPESPermissionException
似乎没问题(或 PesIsNotReadyToWriteException
、WriteToPesPermissionException
,具体取决于您在驼峰标识符中使用缩写的政策)。
请注意,第一个例外中的 Is
绝对是多余的。例如,参见 JDK 异常,例如 ArrayIndexOutOfBoundsException
不是 ArrayIndexIsOutOfBoundsException
。
想到的另一件事是使您的异常更通用一些,例如 PrimaryExternalStorageNotReadyException
(没有准备好任何事情,不仅仅是写)和 PrimaryExternalStoragePermissionException
(实际缺少的权限是写还是读可以作为异常参数传递)。