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

Catch block exception variable null

    Details

    • Type: Bug
    • Status: New
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 11.0.4
    • Fix Version/s: None
    • Component/s: tools
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      The compilation is tested on:
      Linux 64bit - JDK11.0.4
      Windows 32bit - JDK8u221


      A DESCRIPTION OF THE PROBLEM :
      For a precise code pattern, Java compiler makes a bad compilation result, giving null for a catched exception variable. In our production code, it causes NullPointerException when we evoke getMessage() method on it.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the source code (the test code executes well; the original production code in the same circumstances causes a NullPointerException)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Result of decompilation of compiled class:
      public class ForCatchBug
      {
        public static void methodWithException(int paramInt) throws Exception { if (paramInt == 1) throw new Exception("exception");
           }

        
        public static void main(String[] paramArrayOfString) {
          for (b = 0; b < 5; b++) {
            System.out.println(b);
          }
          try {
            methodWithException(1);
          } catch (Exception exception) {
            System.out.println(exception.getMessage());
          }
        }
      }
      ACTUAL -
      Result of decompilation of compiled class:
      public class ForCatchBug
      {
        public static void methodWithException(int paramInt) throws Exception { if (paramInt == 1) throw new Exception("exception");
           }

        
        public static void main(String[] paramArrayOfString) {
          for (b = 0; b < 5; b++) {
            System.out.println(b);
          }
          try {
            methodWithException(1);
          } catch (Exception b) {
            Exception exception; System.out.println(exception.getMessage()); <=== Creation of a new variable
          }
        }
      }

      ---------- BEGIN SOURCE ----------
      import java.lang.Exception;

      public class ForCatchBug {

        public static void methodWithException(int a) throws Exception {
          if (a == 1) throw new Exception("exception");
          return;
        }

        public static void main(String[] args) {
          for (int i = 0; i < 5; i++) {
            System.out.println(i);
          }
          try {
            methodWithException(1);
          } catch (Exception e) {
            System.out.println(e.getMessage());
          }
          return;
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The problem disappears if the part of the code:
          for (int i = 0; i < 5; i++) {
      is replaced by :
          int i = 0;
          for (i = 0; i < 5; i++) {


        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: