3

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.

For example:

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

Some of my custom exception classes extends Exception, they are checked exceptions. The rest of my custom exceptions are unchecked (extends 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. These names are too long for me.

I tried to google for it but I have not found any usefull in this topic.

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?

zappee
  • 20,148
  • 14
  • 73
  • 129
  • 1
    `Error` is something you usually don't recover from and you should not name them `checked/unchecked` - compiler will enforce that. That being said there is `UncheckedIOException` specifically to show that it is like `IOException`, but Unchecked – Eugene Jul 30 '18 at 12:47
  • So you suggest to use something like this: UncheckedAccessDeniedException, AccessDeniedException OR UncheckedInvalidMediaTypeException, InvalidMediaTypeException? This naming seems correct but the class names are so long. – zappee Jul 30 '18 at 12:55
  • The [JLS](https://docs.oracle.com/javase/specs/jls/se10/html/jls-11.html#jls-11.1) distinguishes some kinds of Exceptions. `Error` according to JLS are _exceptions from which ordinary programs are not ordinarily expected to recover_. – LuCio Jul 30 '18 at 12:58

1 Answers1

0

You should not name them checked or unchecked, unless there are two types of these; just like IOException and UncheckedIOException for example.

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; or look into spring - those are some heavy long names.

Eugene
  • 117,005
  • 15
  • 201
  • 306