11 small examples of exception throws
1 PackageDemo;2 3 ImportJava.util.Scanner;4 5 Public classRedtwo {6 7 Public Static voidMain (string[] args) {8 //TODO auto-generated Method Stub9Scanner in =NewScanner (system.in);TenSystem.out.println ("Please enter an integer"); One BooleanStop =true; A Do { - Try { -Integer C =in.nextint (); theSystem.out.println ("The integer is" +c); -Stop =false; -}Catch(RuntimeException ex) { -System.out.println ("The number you enter are not integer"); + //System.out.println (In.next ()); \ - in.nextline (); + } A at} while(stop); - - } - -}
where input.nextline () is critical, if there is no line, the program goes into a dead loop, because after an exception is thrown, the input stream marker is also at the last entered position, and this line of code moves the input stream marker to the next input position.
2016/06/02 Study Record