My team is in the middle of cleaning up our use of throws Exception
and either removing or replacing them with specific exceptions.
A common throws is because an entity was not found. Should we be throwing a generic NotFoundException
or a specific SomeClassNotFoundException
for each entity class?
If we should be throwing a specific exception, should we be creating a specific Exception class for each entity type? Can we safely use generics? Like this class NotFoundException<T extends EntityBaseClass> extends Exception
and then the constructor takes care of declaring what Entity type we're dealing with?
If we should be throwing a specific exception and not using generics, should those exceptions extend or implement a NotFoundException
abstract class or interface?
Java static code analysis: Generic exceptions should never be thrown.
Avoid catching a generic "Exception," "RuntimeException," or "Throwable." A generic exception can arise from too many sources. If you must catch a generic exception in a context that does not allow any exceptions, then arrange to have it rethrown elsewhere.
According to oracle documentation, the following points are the disadvantage of generics: Cannot instantiate Generic types with primitive types. Cannot create instances of type parameters. Cannot declare static fields whose types are type parameters.
Use generic types to maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. The . NET class library contains several generic collection classes in the System.
It's not allowed to make exceptions generic - it won't compile (JLS §8.1.2):
It is a compile-time error if a generic class is a direct or indirect subclass of Throwable
Since type parameters of generics are erased at runtime, there is no way to distinguish between generic exceptions with different type parameters in the catch
clause, thus generic exceptions are not supported. So, you actually have no choice regarding using generic exceptions.
A simple litmus test for the question
Should we be creating a specific Exception class for each entity type?
is
Are there any circumstances where we would need to write a
catch
clause that will catch a "not found" exception thrown by some class X and not by any other class?
If the answer is "yes", then a separate ClassXNotFoundException
is warranted; otherwise, it probably isn't.
As to the second half of your question, the language doesn't permit the use of generics for exception types.
Instead of using:
public Class EntityNotFoundException<T extends EntityBaseClass> extends Exception {
}
You should use:
public Class EntityNotFoundException extends Exception {
private Class<? extends EntityBaseClass> clazz;
public EntityNotFoundException(Class<? extends EntityBaseClass> clazz) {
this.clazz = clazz;
}
. . .
}
This way you can keep a reference to the Entity type that is generating the exception. To throw one of this exceptions you can use:
throw new EntityNotFoundException(Customer.class);
The compiler will validate that Customer does extend EntityBaseClass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With