I analyzed the code I am working on using a Java source code analyzer. One of the warnings is “Always declare user defined exceptions as final”. There are many other warnings that doesn’t make much sense but this one made me a little bit confused.
I am working on a framework and I have one root generic exception (say FrameworkGenericException) and for other exceptions I am simply deriving them from the root exception. So I have a hierarchy of exceptions for the framework. I might extend the hierarchy but this warning I think tells me not to have such hierarchy but define them individually. So what which way should I go, what are your comments?
This is probably standard practice to them: declare classes as final if they are not supposed to be inherited from, and also they probably think that all of your exceptions won’t be extended from.
However, in your case I think that it is a good thing to make one generic exception and extend all others from it. Say, you have:
where these exceptions are sublasses of
FrameworkException. Then you could handle exceptions a little bit easier.So I’d say, you’re doing the right thing, the architecture of your program will be easier to work with.