小安爱偷懒 2021-05-27 08:25 采纳率: 0%
浏览 524

编写一个类Exception Test 1, 在main方法中使用

编写一个类Exception Test 1, 在main方法中使用try, catch, finally 要求 在try块中,编写被0除的代码 在catch块中,捕获被0除所产生的异常,并且打印异常信息 在finally 块中,打印一条语句

  • 写回答

4条回答 默认 最新

  • 关注
    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("程序运行结束.");
    		}
    	}
    
    }
    

    运行结果如下:

    评论

报告相同问题?