Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is result of throwable get.Message ignored in my exception?

I made my own excpetions and I have to print the error message from the exception with a method that uses getMessage(). e.getMessage() is highlighted and it says result of Throwable.getMessage is ignored. For example if I input a number below 5 it prints SmallException caught but doesn’t print Number was below 5.

        try {
            testValue(testNum);
            System.out.println("Number was between 5-10.");
        } catch(SmallException e) {
            System.out.println("SmallException caught!");
            printErrorMessage(e);
        } catch(BigException e) {
            System.out.println("BigException caught!");
            printErrorMessage(e);
        }
    }
    public static void printErrorMessage(Exception e) {
        e.getMessage();
    }

    public static void testValue(int num) throws SmallException, BigException {
        if(num < 5) {
            throw new SmallException("Number was below 5.");
        } else if(num > 10) {
            throw new BigException("Number was over 10.");
        }
    }
}

class SmallException extends Exception {
    SmallException() {
        super();
    }

    SmallException(String message) {
        super(message);
    }

}

class BigException extends Exception {
    BigException() {
        super();
    }

    BigException(String message) {
        super(message);
    }
}

I can’t think of anything to try 🙁

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re not printing the message. You’re just calling the getter (which is useless and why your IDE is indicating "result of Throwable.getMessage is ignored").

public static void printErrorMessage(Exception e) {
    e.getMessage();
}

Should be:

public static void printErrorMessage(Exception e) {
    System.out.println(e.getMessage());
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading