Pergunta

I am learning features in java including exceptions. I am writing a custom exceptions. Here is what i am doing :custom exception class:

 public class ServiceException extends Exception {

    private String customMessage;

    public ServiceException(String customMessage) {
        super(customMessage);
        this.customMessage = customMessage;

    }
}

Main class:

    public class Main {

    public static void main(String[] args) {
        try {
            new Main().test();
        } catch (Exception e) {
            System.out.println("the exception message is " + e.getMessage());
        }
    }

    public void test() throws ServiceException {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}

This much i know: if super class constructor is not invoked in the custom exception class, the message set in the custom exception is not passed to the Exception class. But if i have a method public String getMessage in my custom exception class, even if the super is not invoked, that message is printed . Sorry if this is a naive question. But i am failing to understand he concept. Could come one help clear the concept ?

Foi útil?

Solução

In main where you are catching the error, you are basically assigning a ServiceException object to a Exception reference, i.e. assigning derived class object to base class reference, so if the derived class has overridden the method, it will get called.

the e.message() being called is from ServiceException not Exception, you are right, no data is being passed when you are not calling super, data is inside ServiceException class only and the function invoked is also from ServiceException class.

Outras dicas

That is because you are supplying it. You are passing e.getMessage() to your constructor as the only argument, customMessage. You then pass customMessage to its parent's constructor that takes a String, Exception(String). In doing so, you are giving it the message to use for serviceExceptionInstance.getMessage(). Instead, do not pass the customMessage to its parent (use super();, which is implied if no call to a parent constructor is given and a no-arg, parent constructor exists). Then the message will be null as it is not supplied.

In other words:

new ServiceException(e.getMessage());

Creates a new ServiceException with the message from e. You pass that message to Exception, ServiceException's parent.

super(customMessage);

In doing so, you use the single argument, String-based constructor of Exception. Passing a value to that constructor implies that you want it used when callers invoke getMessage. To avoid doing this, call a different parent constructor, or none at all (calling none is technically not possible, and it will implicitly do super(); for you):

public ServiceException(String customMessage)
{
    this.customMessage = customMessage;
}

This will call super(); for you, which means that the parent class has no message to send, and by not overriding getMessage() yourself, then it will return its default value (null). To be clear, Exception itself extends from Throwable, which is really the class providing this functionality, but it all stems from how you work with Exception as it serves as a pass-thru to Throwable.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top