1

I'm adding Exception handling to my Java application and don't understand why in some cases the String returned by Exception methods e.getMessage() or e.getLocalizedMessage() includes the package of my custom Exception class.

This is how I have it configured:

MyCustomException:

package project.be.exception;

public class MyCustomException extends Exception{
 public MyCustomException() {

}

public MyCustomException(String arg0) {
    super(arg0);

}

public MyCustomException(Throwable arg0) {
    super(arg0);

}

public MyCustomException(String arg0, Throwable arg1) {
    super(arg0, arg1);

}

public MyCustomException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
    super(arg0, arg1, arg2, arg3);

}
}

MyServiceCaller:

public class MyServiceCaller implements HISException DefaultRESTService<DefaultModel>{


    public DefaultResponse get(Service_Api service, Object caller, Object param) throws MyCustomException {

        try{
         ...
        } catch (Exception e){
           throw new MyCustomException ("Hello message exception");
        }

        }
}

MyBussinessClass:

...
try{
 new MyServiceCaller().get(service, param1, param2);
} catch(MyCustomException e){
   System.out.println(e.getLocalizedMessage());
}
...

Console output:

project.be.exception: Hello message exception

I want to print only the message without previous the package. Any suggestion?

Edit1:

The output is the same using Exception.getMessage(), thus I discard possible duplication with question: Difference between e.getMessage() and e.getLocalizedMessage()

SOLUTION:

As isaac mentioned, I'm wraping the thrown exception in another and by this reason e.getMessage() and e.getLocalizedMessage() shows the package.

In my case solve the output was easy calling e.getCause().getMessage() instead of e.getMessage() and e.getLocalizedMessage().

8
  • @devpuh the issue it identical with e.getMessage()
    – bishop
    Commented Feb 13, 2018 at 10:30
  • The code for the constructor MyCustomException is missing. Also do you override getMessage() or getLocalizedMessage()?
    – user8097737
    Commented Feb 13, 2018 at 10:32
  • Why not use a logger instead of sysout ? For instance, java.util.logging .
    – Mickael
    Commented Feb 13, 2018 at 10:40
  • @devpuh getMessage() or getLocalizedMessage() aren't overrided
    – bishop
    Commented Feb 13, 2018 at 10:45
  • 2
    You may be facing the issue described here, i.e. you're somehow, inadvertently, wrapping the thrown exception in another, especially since you say it only happens "sometimes". I think it'd helpful if you could provide a working and a non-working example.
    – cosh
    Commented Feb 13, 2018 at 10:45

1 Answer 1

0

I recommend you to use a logging system instead of using the Java SysOut : System.out.println(...). With a logging system, you can define the format of your logs.

Here's an example with java.util.logging :

You can define your own logging format. Here's an example of Formatter that will only display the message of any Exception :

public class MyFormatter extends Formatter {

    public String format(LogRecord record) {
        StringBuilder builder = new StringBuilder();
        builder.append(formatMessage(record));
        builder.append("\n");
        return builder.toString();
    }
}

Then you can use it as following :

public class MyClass {

    // Your logger
    private static Logger logger ;

    // Static initialization block for the logger
    static {
        logger = Logger.getLogger("MyClass");
        try {
            ConsoleHandler handler = new ConsoleHandler();
            handler.setFormatter(new MyFormatter());
            logger.addHandler(handler);
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Failed to initialize Handler", e);
        }
    }

    //...
}

Please note that's not proper way to configure your logging system. The point is to show how it works. In a real application, you can use system properties, configuration files, etc...

In order to configure your logger correctly, there're several solutions. Take a look to this Stack overflow question :

I hope this will help you.

1
  • I've using java.util.logging in my application but I thought that was more clear SysOut for the question. Thanks for the recommendation.
    – bishop
    Commented Feb 13, 2018 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.