Java – “do not allow exceptions to be caught” checkstyle Report

I'm generating a checkstyle report embedded in Maven site, and one of the problems is that it indicates that exceptions are not allowed to be caught How can I solve this problem? I just don't want to simply delete the code if I don't have another alternative to solve this problem

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'm still learning Java, so any form of guidance will be appreciated

thank you

Solution

It warns you that catching exceptions is a bad idea Exceptions are the most common types of exceptions you can catch You're basically saying, "no matter what problem you encounter, I can handle it." It is not true. There may be any strange and wonderful problems: the keyboard is interrupted, the disk space is full, and the list continues

You said that loadclientuserdata threw a managerexception, so you should catch that specific exception and let any other exception 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:

> Is it really that bad to catch a general exception? > In Java,what is the difference between catch a generic exception and a specific exception (eg. IOException?)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>