Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8367990

Math XXExact intrinsics throw ArithmeticException with a null message

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P2 P2
    • None
    • 25
    • hotspot
    • None
    • 25
    • generic
    • generic

      While the specification for `Throwable::getMessage` allows for a null message, it seems odd that the Math XXXExact intrinsics throw an ArithmeticException with a null message.

      This is a change in behaviour between JDK 24 and JDK 25.

      ```
      $ cat TestMathAddExactOverflow.java
      public class TestMathAddExactOverflow {

       public static void main(String... args) {
          for (int i = 0; i < 20_000; i++) {
            addExactImpl(Integer.MAX_VALUE, i);
          }
        }

        static int addExactImpl(int a, int b) {
          try {
            return Math.addExact(a, b);
          } catch (ArithmeticException e) {
            if (e.getMessage() == null) {
              var x = new AssertionError("null message, b=" + b);
              x.addSuppressed(e);
              throw x;
            }
         }
         return -1;
        }
      }
      ```

      $ ./jdk-24..0.2.jdk/Contents/Home/bin/java TestMathAddExactOverflow

      $ ./jdk-25.jdk/Contents/Home/bin/java -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_addExactI TestMathAddExactOverflow

      $ ./jdk-25.jdk/Contents/Home/bin/java TestMathAddExactOverflow
      Exception in thread "main" java.lang.AssertionError: null message, b=7745
      at TestMathAddExactOverflow.addExactImpl(TestMathAddExactOverflow.java:14)
      at TestMathAddExactOverflow.main(TestMathAddExactOverflow.java:5)
      Suppressed: java.lang.ArithmeticException

      Math:: subtractExact shows similar behaviour too.

            Unassigned Unassigned
            chegar Chris Hegarty
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: