Asking for a number in Java Swing Is it good way of asking for a number? Should that function return 0 if there is an error?

private int getNumberDialog(String message) {
            try {
                String resultInString = JOptionPane.showInputDialog(message);
                int result = Integer.parseInt(resultInString);
                return result;
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "You didn't gave a number");
            }
            return 0;
        }
Avoid using "special return values" in code for when errors occur. It should be up to the calling code how to handle the situation.

I would let the getNumberDialog utility function re-throw the exception:

private int getNumberDialog(String message) throws NumberFormatException {
    String resultInString = JOptionPane.showInputDialog(message);
    int result = Integer.parseInt(resultInString);
    return result;
}


And in your usage code, you catch the exception:

try {
    int times = getNumberDialog("How many times?");
    game.playForMeNrTimes(times);
} catch (NumberFormatException ex) {
    JOptionPane.showMessageDialog(null, "You didn't gave a number");
}


The key here is that the utility-function getNumberDialog doesn't know about any context. It doesn't know how the returned value will be used, so how can it possibly decide on a default value to use in the case of an error?