Continue a while loop after exception

前端 未结 6 498
春和景丽
春和景丽 2021-01-16 12:39

i have this piece of code. I wanted to return to the beginning of loop and ask for user input again. However, it always loops without stopping to ask for input. What is wron

6条回答
  •  借酒劲吻你
    2021-01-16 12:59

    This should throw and catch the exception and the continue command should send you back to your while loop. you need either a continue or a flag to tell your while when it stops being true.

    while(true)
    {
    try 
    {
       int choice = input.nextInt();
       throw new InputMismatchException();
    
    } 
    catch (InputMismatchException e)
    {
    continue;
    }
    }
    

提交回复
热议问题