try块:
如果不希望方法结束的话,将方法用try语句块包裹起来,再进行各种(课程产生异常的)方法调用。
将会产生两种处理机制:参考12.2
1,异常处理机制
2,方法继续执行
try的语法
try{}catch(){}catch(){}...try{}finally{}
终止与恢复模型
如果想要用Java实现类似恢复的行为,那么在遇见错误的时候都不能抛出异常
public class Exception2 {public static void main(String[] args) {System.out.println("请输入number ");Scanner scanner = null;while (true) {scanner = new Scanner(System.in);try {int temp = scanner.nextInt();System.out.println(temp);} catch (InputMismatchException e) {//throw new InputMismatchException("错误得输入");//只要抛出异常 后续代码都无法执行System.out.println("请输入正确得格式,格式为数字类型");scanner.reset();continue;//在方法尾部加上continue 加不加本次方法都会结束 继续下次循环}System.out.println("while循环块内得代码");}}}
