Skip to content

Instantly share code, notes, and snippets.

@S64
Last active August 29, 2015 14:18
Show Gist options
  • Save S64/147679635709387b7022 to your computer and use it in GitHub Desktop.
Save S64/147679635709387b7022 to your computer and use it in GitHub Desktop.
ゼロ除算のテスト。
class DivisionByZero {
protected static final int[] NUMBERS = {8 , -8, 0};
public static void main(String[] args) throws java.lang.Exception {
for (Mode m: Mode.values()) {
for (NumberMode nM : NumberMode.values()) {
try {
ExecCalc(m, nM);
} catch (Exception e) {
System.out.println();
e.printStackTrace(System.out);
} finally {
System.out.println();
}
}
}
}
protected static void ExecCalc(Mode mode, NumberMode nMode) {
int number;
switch(nMode) {
case Plus:
number = NUMBERS[0];
break;
case Minus:
number = NUMBERS[1];
break;
case Zero:
default:
number = NUMBERS[2];
break;
}
switch (mode) {
case UseInteger:
ExecCalc(Integer.valueOf(number));
break;
case UseDouble:
ExecCalc((double) number);
break;
case UseShort:
ExecCalc((short) number);
break;
case UseLong:
ExecCalc((long) number);
break;
case UseFloat:
ExecCalc((float) number);
break;
}
}
protected static void ExecCalc(int number) {
System.out.print("Integer: "+number+" / 0 = ");
System.out.println(number / 0);
}
protected static void ExecCalc(double number) {
System.out.print("Double: "+number+" / 0 = ");
System.out.println(number / 0);
}
protected static void ExecCalc(short number) {
System.out.print("Short: "+number+" / 0 = ");
System.out.println(number / 0);
}
protected static void ExecCalc(long number) {
System.out.print("Long: "+number+" / 0 = ");
System.out.println(number / 0);
}
protected static void ExecCalc(float number) {
System.out.print("Float: "+number+" / 0 = ");
System.out.println(number / 0);
}
protected static enum Mode {
UseInteger,
UseDouble,
UseShort,
UseLong,
UseFloat
}
protected static enum NumberMode {
Plus,
Minus,
Zero
}
}
/*
Integer: 8 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:55)
at DivisionByZero.ExecCalc(Main.java:36)
at DivisionByZero.main(Main.java:9)
Integer: -8 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:55)
at DivisionByZero.ExecCalc(Main.java:36)
at DivisionByZero.main(Main.java:9)
Integer: 0 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:55)
at DivisionByZero.ExecCalc(Main.java:36)
at DivisionByZero.main(Main.java:9)
Double: 8.0 / 0 = Infinity
Double: -8.0 / 0 = -Infinity
Double: 0.0 / 0 = NaN
Short: 8 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:65)
at DivisionByZero.ExecCalc(Main.java:42)
at DivisionByZero.main(Main.java:9)
Short: -8 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:65)
at DivisionByZero.ExecCalc(Main.java:42)
at DivisionByZero.main(Main.java:9)
Short: 0 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:65)
at DivisionByZero.ExecCalc(Main.java:42)
at DivisionByZero.main(Main.java:9)
Long: 8 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:70)
at DivisionByZero.ExecCalc(Main.java:45)
at DivisionByZero.main(Main.java:9)
Long: -8 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:70)
at DivisionByZero.ExecCalc(Main.java:45)
at DivisionByZero.main(Main.java:9)
Long: 0 / 0 =
java.lang.ArithmeticException: / by zero
at DivisionByZero.ExecCalc(Main.java:70)
at DivisionByZero.ExecCalc(Main.java:45)
at DivisionByZero.main(Main.java:9)
Float: 8.0 / 0 = Infinity
Float: -8.0 / 0 = -Infinity
Float: 0.0 / 0 = NaN
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment