Java methods and exception handling

Integer.parseInt converts decimal text within the int range. Invalid syntax or an out-of-range value throws NumberFormatException.

Java
class Main {
    static void showNumber(String text) {
        try {
            int value = Integer.parseInt(text);
            System.out.println(value * 2);
        } catch (NumberFormatException error) {
            System.out.println("Invalid integer");
        }
    }
    public static void main(String[] args) {
        showNumber("12");
        showNumber("abc");
    }
}
Read the full lesson