Newer
Older
java_learning / java-learning / src / jp / co / jid / exceptionSample / Exercises01.java
himeno on 2 Aug 2019 1 KB init
package jp.co.jid.exceptionSample;

import java.util.ArrayList;

public class Exercises01 {

	public static void main(String[] args) {
		ArrayList<String> inputStrs = InputUtil.input();
		String str1 = inputStrs.get(0);
		String str2 = inputStrs.get(1);
		try {
			System.out.println(str1 + "÷" + str2 + "の除算結果は" + div(str1, str2));
		} catch (NumberFormatException e) {
			System.out.println("数字ではありません");
		} catch (NullPointerException e) {
			System.out.println("値が指定されていません");
		} catch (ArithmeticException e) {
			System.out.println("∞");
		}
	}

	private static double div(String str1, String str2) throws NumberFormatException, ArithmeticException {
		if (str1 == null && str2 == null) {
			throw new NullPointerException();
		}
		double num1 = (str1 == null ? 0.0 : Double.valueOf(str1));
		double num2 = (str2 == null ? 0.0 : Double.valueOf(str2));
		if (num2 == 0) {
			throw new ArithmeticException();
		}
		return num1 / num2;
	}
}