"No exception is not allowed."

I am generating a report Checkstyle

embedded in the maven site and for one of the problems it indicates that Catching exception is not allowed

. How can I fix this problem? I just don't just want to remove the code, and if I have no other alternatives to fix this issue.

public void contextInitialized(ServletContextEvent event) {
    super.contextInitialized(event);

    ServletContext context = event.getServletContext();
    setupContext(context);
    LoggingHandler logging = (LoggingHandler) AppContext.getBean( "loggingHandler" );

    try {
        loadClientUserData( context, logging );
        loadMBeans( context, logging );

    } catch (Exception e) {
        throw new RuntimeException( "Error during startup of service !!!" );
    }
}

      

I am still studying Java

so any guidance would be appreciated.

thank

+3


source to share


3 answers


What do you warn that agility Exception

is a bad idea. Exception

is the most common type of exception you can catch. You basically say, "What a problem, I can handle this." It is not true. There can be a lot of weird and wonderful problems out there: keyboard interruptions, full disk space, the list goes on.

You said it loadClientUserData

throws ManagerException

, so you should catch that specific exception and leave any others to propagate further:

try {
     loadClientUserData( context, logging );
     loadMBeans( context, logging );
} catch (ManagerException e) {
     throw new RuntimeException( "Error during startup of service !!!" );
}

      



For more information, see the following questions:

+5


source


You catch Generic Exception and do nothing about what seems to be a problem with Checkstyle. You can either rebuild the exception, handle it better, or ignore the Checkstyle check by creating a deny file.



+2


source


You should catch the narrowest subclass of Exception you can, for reasons that are too long to explain here, but in a nutshell, Exception includes thrown exceptions.

If your methods are declared as throws Exception

, that is also a style issue.

0


source







All Articles