异常

1.try-catch实现异常捕捉

public class ExceptionDemo {
public static void main(String[] args) {
try {
Scanner input = new Scanner(System.in);
System.out.println("请输入被除数:");
int num1 = input.nextInt();
System.out.println("请输入除数:");
int num2 = input.nextInt();
}catch (InputMismatchException e) {
System.out.println("输入的数据不为整数");
}catch (ArithmeticException e) {
System.out.println("除数不能为0");
}catch (Exception e) {

}finally {
System.out.println("欢迎使用本程序");
}
}

}

posted @ 2016-11-03 14:20  造西  阅读(62)  评论(0编辑  收藏  举报