Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it wise to use generics for exceptions?

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?

like image 755
Freiheit Avatar asked Jul 25 '11 15:07

Freiheit


People also ask

Should I throw generic exception?

Java static code analysis: Generic exceptions should never be thrown.

Should we catch generic exception Java?

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.

What is the disadvantages of using generics?

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.

Should you use generics?

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.


3 Answers

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.

like image 106
axtavt Avatar answered Oct 23 '22 21:10

axtavt


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.

like image 42
NPE Avatar answered Oct 23 '22 22:10

NPE


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.

like image 8
Marcelo Avatar answered Oct 23 '22 21:10

Marcelo