编写一个类Exception Test 1, 在main方法中使用try, catch, finally 要求 在try块中,编写被0除的代码 在catch块中,捕获被0除所产生的异常,并且打印异常信息 在finally 块中,打印一条语句
4条回答 默认 最新
CSDN专家-赖老师(软件之家) 2021-05-27 10:00关注import java.util.Scanner; public class ExceptionTest1 { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入2个整数."); int a= input.nextInt(); int b = input.nextInt(); try { a = a/b; }catch(Exception e) { System.out.println("分母不能为0"); }finally { System.out.println("程序运行结束."); } } }运行结果如下:
评论 打赏 举报解决 3无用