Exception handlers should preserve the original exception : Either log or rethrow this exception

7.9k Views Asked by At

This is my method, when I try to analyze my code by sonarQube am getting this error:

Exception handlers should preserve the original exception : Either log or rethrow this exception.

Why am getting this error, should I not catch the exception like my method?

my method :

for (String QT : Q_T) {

                try {
                        // some logic 
                    }

                } catch (JsonParseException e) {                    
                    LOG.log(Level.SEVERE, e.toString());
                } catch (JsonMappingException e) {
                    LOG.log(Level.SEVERE, e.toString());
                } catch (IOException e) {
                    LOG.log(Level.SEVERE, e.toString());
                }
                catch (Exception e) {
                    LOG.log(Level.SEVERE, e.toString());
                }
            }
        }
1

There are 1 best solutions below

0
On BEST ANSWER

I believe what it's trying to tell you is to log the Exception as it is, not the toString() version, like here, also adding some 'context' or information to the log

for (String QT : Q_T) {
        try {
            // some logic 
        } catch (JsonParseException e) {                    
            LOG.log(Level.SEVERE, "context", e);
        } catch (JsonMappingException e) {
            LOG.log(Level.SEVERE, "context", e);
        } catch (IOException e) {
            LOG.log(Level.SEVERE, "context", e);
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "context", e);
        }
}