java中用try如果catch到异常,怎么把这个异常处理掉让程序继续运行
2014-10-11 20:30
645 查看
需要先将Scanner内的输入清楚,不然就重复读取了。简单调用Scanner 的next()方法就可以了
测试已通过
package zhidao;
import java.util.Scanner;
public class Retry {
private int myInt;
private Scanner myScanner = new Scanner(System.in);
public int getInt() {
try {
System.out.println("请输入法整数");
this.myInt = this.myScanner.nextInt();
} catch (Exception e) {
System.out.println("输入错误,请输入正确的数值!!!");
this.myScanner.next();
this.getInt();
}
return this.myInt;
}
public static void main(String[] args) {
Retry retry = new Retry();
System.out.println(retry.getInt());
}
}
测试已通过
package zhidao;
import java.util.Scanner;
public class Retry {
private int myInt;
private Scanner myScanner = new Scanner(System.in);
public int getInt() {
try {
System.out.println("请输入法整数");
this.myInt = this.myScanner.nextInt();
} catch (Exception e) {
System.out.println("输入错误,请输入正确的数值!!!");
this.myScanner.next();
this.getInt();
}
return this.myInt;
}
public static void main(String[] args) {
Retry retry = new Retry();
System.out.println(retry.getInt());
}
}
相关文章推荐
- java异常处理和自定义异常利用try和catch让程序继续下去(回来自己再写个例子试运行下)
- java异常处理和自定义异常利用try和catch让程序继续下去
- java 程序中try catch / throw / 函数名上throws 具体执行顺序和扔异常捕获异常的概念模型
- 【RuntimeException运行时异常】方法不需要throws、调用该方法的程序不需要try-catch
- msgbox框出来以后,如果一定时间内没有人点确定,那怎么让这个框消失,使程序自动运行?
- 0016 Java学习笔记-异常-如果try-catch-finally中都存在return语句会怎样?
- 编写一个java程序,从1加到100,每做一次加法,就休眠1秒,当程序运行过程中,杀死这个程序。 再次运行,还能从上次的计算结果继续下去
- Try,Catch,Finally三块中如果有Return是怎么个运行顺序
- Java中已经被try,catch的异常再其上一层中如何继续被try,catch
- JAVA 处理程序异常,(try、catch、finally),(thorws)
- 编写一个java程序,从1加到100,结果是5050。 每做一次加法,就休眠1秒 当程序运行过程中,杀死这个程序。 再次运行,还能从上次的计算结果继续下去。
- java的异常处理机制(try…catch…finally)
- BlackBerry手机上面开发Java程序,如果才能让代码运行效率高,占用系统资源少?
- 技巧(5)- 如何让这个程序在系统里同时只运行一个(如果这个exe程序已经运行了,就不再运行)
- [Java] try - catch 异常处理机制
- java的异常处理机制(try…catch…finally)
- [Java] try - catch 异常处理机制
- java的异常处理机制(try…catch…finally)
- java,异常处理,throwable和try...catch[zz]
- 关于java异常处理的几个关键字 try catch/throw/throws