'how to catch input exception for a scanner [duplicate]

I need to scan a number and then sort it in a list. How can i make the program ignore if a user types in letters instead of a number. it throws

Exception in thread "main" java.util.InputMismatchException.

code:

public static void InsertionSort() {

        LinkedList<Integer> list = new LinkedList<>();
        Scanner scan = new Scanner(System.in);
        
        int x=-2;
        while(x!=-1)
        {   
            System.out.println("type a number to place in a sorted list");
            x=scan.nextInt();
        }
        scan.close();
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source