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

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

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

}

 

运行结果如下:

public class ExceptionTest1{
public static void main(String args[]){
try{
int n=7;
int m-0;
int div=n/m;
}catch(Exception e) {
e.printstack();
}finally{
System.out.println("this game is over");
}
}
}

记得点个采纳,谢谢!

package gz.ht.pojo;

import java.util.Scanner;

public class ExceptionTest1 {
	public static void main(String[] args) {
		int a,b;
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入一个整数");
		a=sc.nextInt();
		System.out.println("请输入一个整数");
		b=sc.nextInt();
		try {
			int c=a/b;
		} catch (ArithmeticException e) {
			e.printStackTrace();
		}finally {
			System.out.println("我是断后的...");
		}
	}
}

代码和测试结果如上,万望采纳。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632