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().

bishop
  • 360
  • 5
  • 21
  • @devpuh the issue it identical with e.getMessage() – bishop Feb 13 '18 at 10:30
  • The code for the constructor `MyCustomException` is missing. Also do you override `getMessage()` or `getLocalizedMessage()`? –  Feb 13 '18 at 10:32
  • Why not use a logger instead of sysout ? For instance, [java.util.logging](https://docs.oracle.com/javase/8/docs/api/java/util/logging/package-summary.html) . – Mickael Feb 13 '18 at 10:40
  • @devpuh getMessage() or getLocalizedMessage() aren't overrided – bishop Feb 13 '18 at 10:45
  • 2
    You may be facing the issue described [here](https://stackoverflow.com/questions/9017820/exception-getmessage-output-with-class-name), 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 Feb 13 '18 at 10:45
  • @MickaëlB using logger (java.util.logging) the issue is exactly the same – bishop Feb 13 '18 at 10:47
  • @isaac you're right. I'm wrapping the thrown exception in another. – bishop Feb 13 '18 at 11:09
  • I don't think the solution you mentionned works in all cases because an exception can have more than one cause.... – Mickael Feb 13 '18 at 11:32

1 Answers1

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.

Mickael
  • 4,458
  • 2
  • 28
  • 40
  • 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 Feb 13 '18 at 11:42