简体   繁体   English

Java中已检查和未检查异常的命名约定

[英]Naming convention for checked and unchecked exceptions in Java

I am writing an application wehre I have some custom exception classes. 我在编写应用程序,但有一些自定义异常类。 The naming convention what I use for the custom exception classes is <error-name>Error.java . 我用于自定义异常类的命名约定是<error-name>Error.java

For example: 例如:

  • AccessDeniedError.java AccessDeniedError.java
  • InvalidMediaTypeError.java InvalidMediaTypeError.java
  • EmptyRequestBodyError.java EmptyRequestBodyError.java
  • ... ...

Some of my custom exception classes extends Exception , they are checked exceptions. 我的一些自定义异常类扩展了Exception ,它们是已检查的异常。 The rest of my custom exceptions are unchecked (extends RuntimeException ). 我其余的自定义异常未选中(扩展了RuntimeException )。

My naming convention does not reflect that so when someone only see the name of the exception then he is not able to decide whether it is a checked or unchecked exception. 我的命名约定没有反映出这一点,因此当某人仅看到该异常的名称时,他就无法确定该异常是已检查还是未检查。

I am not sure if the class name needs to reflect the type of the exception or not, but the naming I use does not tells anything about it. 我不确定类名是否需要反映异常的类型,但是我使用的命名并不能说明任何有关异常的信息。

I would like to keep the classnames as short as possible so I DO NOT want to use names like AccessDeniedCheckedException or AccessDeniedUncheckedException . 我想使类名尽可能短,所以我不想使用诸如AccessDeniedCheckedExceptionAccessDeniedUncheckedException之类的名称。 These names are too long for me. 这些名字对我来说太长了。

I tried to google for it but I have not found any usefull in this topic. 我尝试用Google搜索它,但是在此主题中没有发现任何有用的信息。

Can I continue using this naming or better to rename my custom exception classes in order to they show the type of the exception as well? 我是否可以继续使用此命名或更佳的名称来重命名我的自定义异常类,以便它们也显示异常类型?

You should not name them checked or unchecked , unless there are two types of these; 除非有两种类型, 否则不应将它们命名为“ checked或“ unchecked just like IOException and UncheckedIOException for example. 就像IOExceptionUncheckedIOException一样。

Otherwise users will know what types these are with the help of the compiler, obviously. 否则,显然,在编译器的帮助下,用户将知道这些类型是什么。

About the length of the class name, well, JDK itself has some names that will scare you; 关于类名的长度,JDK本身有一些名称会让您感到恐惧。 or look into spring - those are some heavy long names. 或展望春天-这些都是冗长的名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM