0

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 :(

1 Answers1

3

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());
}
nickb
  • 59,313
  • 13
  • 108
  • 143