Java try and catch until no exception is thrown
<p>I am trying to get an integer from the user using a Scanner and an infinite loop. I know a solution to solve this problem but I keep wondering why doesn't my first approach work properly?</p>

<pre><code>Scanner myScanner = new Scanner(System.in);
int x = 0;
while(true){
    try{
        System.out.println("Insert a number: ");
        x = myScanner.nextInt();
        break;
    }catch(InputMismatchException e){
        System.out.println("Invalid, try again.");
        continue;
    }
}
</code></pre>

<p>It works on the first iteration but then it just keeps printing "Invalid, try again" on the screen forever.</p>