-
Bug
-
Resolution: Cannot Reproduce
-
P4
-
None
-
11.0.4
-
generic
-
generic
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++) {
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++) {