题目:
编写应用程序,从命令行输入两个整数参数,求他们的商,要求程序中捕获所有可能发生的异常

异常

  1. NumberFormatException 数字格式异常,判读输入的数字是否是数字
  2. ArithmeticException 算术异常,判断除数是否为0
public class TestParameter {
   

	public static void main(String[] args) {
   

		// ArithmeticException 算数异常
		// NumberFormatException 数字格式异常
		Scanner scanner = new Scanner(System.in);
		while (true) {
   
			try {
   
				System.out.println("请输入第一个数:");
				int n1 = Integer.parseInt(scanner.nextLine());
				System.out.println("请输入第二个数:");
				int n2 = Integer.parseInt(scanner.nextLine());
				int n3 = n1 / n2;
				System.out.println(n1 + " / " + n2 + " = " + n3);
			} catch (NumberFormatException e) {
   
				e.printStackTrace();
				continue;
			} catch (ArithmeticException e) {
   
				e.printStackTrace();
				continue;
			} finally {
   
				System.out.println("每次都输出的!");
			}
		}
	}
}

运行结果:

每天都在进步!!!