本人刚学Java,遇到了一个难以解决的问题
代码如下:
public class test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("输入物品价格");
int prise = sc.nextInt();
System.out.println("价格合理");
} catch (Throwable e) {
e.printStackTrace();
System.out.println("数据不合法");
}
System.out.println("输入a");
int a = sc.nextInt();
System.out.println("输入b");
int b = sc.nextInt();
}
}
具体问题:
如果输入的prise为int类型,不触发异常的话,则接下来的代码都能正常运行,如果输入的prise为string类型或者别的类型,在执行了catch内部的语句后,当执行到int a = sc.nextint()时会直接结束进程,具体图片如下: