java - Continue Program Execution after Catching Exception -


after catching exception, how continue execution of java program?

i made program asks user enter number , return number divided random number generated. however, if user enters letter 'a', exception caught.

how make program continue execution instead of terminating after catches exception?

do{                     //begin of loop      try{                        //try code          system.out.println("enter number");          double = read.nextdouble(); //reads user input           double rn = r.nextint(10);   //generates random number rn          system.out.println(i +  " divided random number " + rn + " " + (i/rn));      }catch(inputmismatchexception type_error){         //catches error if there type mismatch         //example error: if user enters letter instead of double          system.out.println("error. cannot divide letter number!");          break;  //break stops execution of program      }       //using continue statement here not work  }while(true);       //loop forever  

continue won't help! if use scanner class input numbers, got infinite loop writing "error. cannot divide letter number!" output.

your code waits double number, got letter. event triggers exception, code displays error message. letter remains in scanner, nextint command tries load same letter in next iteration, without wait typing.

in catch block, have empty scanner read.next() command.

    scanner read = new scanner(system.in);     random r = new random();      {                     //begin of loop           try {                        //try code              system.out.println("enter number");             double = read.nextdouble(); //reads user input               double rn = r.nextint(10);   //generates random number rn              system.out.println(i + " divided random number " + rn + " " + (i / rn));          } catch (inputmismatchexception type_error) {         //catches error if there type mismatch             //example error: if user enters letter instead of double             system.out.println("error. cannot divide letter number!");              // empty scanner before next iteration:              read.next();         }      //using continue statement here not work      } while (true);       //loop forever  

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -