Math XXExact intrinsics throw ArithmeticException with a null message

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P2
    • None
    • Affects Version/s: 25
    • Component/s: 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.

            Assignee:
            Unassigned
            Reporter:
            Chris Hegarty
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: