How to fix the following error in Catch block of Java code?

188 views Asked by At

I have a code snippet which is throwing error when using printStackTrace() under catch block of my code. Following is the code snippet.

         try
     {
            # Debug Code added on 19 Feb 2016
            logger.log(Level.INFO, "baseDNs[i] = "+baseDNs[i]);
            logger.log(Level.INFO, "search ="+search);
            logger.log(Level.INFO, "attributes = "+attributes);

        it = basicCmAgent.find_managed_objects(baseDNs[i], search, attributes);
     }
     catch(Exception e)
     {
        # Debug Code added on 19 Feb 2016
        logger.log(Level.SEVERE, "Caught Error : "+e.printStackTrace());

        logger.log(Level.WARNING, "Could not find managed objects with base DN " + baseDNs[i]);
        return false;
     }

Following are the errors:

       asses/xml-apis.jar:../3pp_libraries/cxa_classes/irp3gppR99_330_j140.jar -d lib com/ericsson/nms/temip/importer/BasicCmConnection.java
    com/ericsson/nms/temip/importer/BasicCmConnection.java:177: 'void' type not allowed here
            logger.log(Level.SEVERE, "Caught Error : " +e.printStackTrace());
                                     ^
Note: ./com/ericsson/nms/temip/importer/BasicCmConverter.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
*** Error exit code 1

Stop.
*** Error exit code 1

Stop.

How can i fix this?

3

There are 3 answers

0
Eran On

printStackTrace() has a void return type, so you can't concatenate it to a String or use it as an argument for another method.

You might want to use getStackTrace() instead.

logger.log(Level.SEVERE, "Caught Error : "+e.getStackTrace());
0
William Rosenbloom On

e.printStackTrace() does not return a String it returns void and prints the stack trace. You are trying to do String + void = String which does not work.

0
Nasreen On

The issue is at this line

 logger.log(Level.SEVERE, "Caught Error : "+e.printStackTrace());

The return type of e.printStackTrace is void. You cannot concat String +void.

Better use e.getStactTrace();