Community
Participate
Working Groups
3.2M5 On the following test case, the finally block is executed twice: [try]null[finally] [npe]null[finally] Exception in thread "main" java.lang.NullPointerException at X.save(X.java:22) at X.main(X.java:26) public class X { public void save() { try { Object warnings = null; Object contexts = null; try { System.out.print("[try]"); System.out.print(warnings); return; } catch (NullPointerException npe) { System.out.print("[npe]"); System.out.print(contexts); return; } } catch (Exception e) { System.out.print("[e]"); return; } finally { int var = 0; System.out.println("[finally]"); Object o = null; o.toString(); } } public static void main(String[] args) { new X().save(); } }
The problem arises from the nested try with no finally block. Exception handlers are not properly closed then. The range for NPE handler is far too long: // Method descriptor #6 ()V // Stack: 2, Locals: 7 public void save(); 0 aconst_null 1 astore_1 [warnings] 2 aconst_null 3 astore_2 [contexts] 4 getstatic System.out : PrintStream [15] 7 ldc <String "[try]"> [21] 9 invokevirtual PrintStream.print(String) : void [23] 12 getstatic System.out : PrintStream [15] 15 aload_1 [warnings] 16 invokevirtual PrintStream.print(Object) : void [29] 19 iconst_0 20 istore 5 [var] 22 getstatic System.out : PrintStream [15] 25 ldc <String "[finally]"> [32] 27 invokevirtual PrintStream.println(String) : void [34] 30 aconst_null 31 astore 6 [o] 33 aload 6 [o] 35 invokevirtual Object.toString() : String [37] 38 pop 39 return 40 astore_3 [npe] 41 getstatic System.out : PrintStream [15] 44 ldc <String "[npe]"> [41] 46 invokevirtual PrintStream.print(String) : void [23] 49 getstatic System.out : PrintStream [15] 52 aload_2 [contexts] 53 invokevirtual PrintStream.print(Object) : void [29] 56 iconst_0 57 istore 5 [var] 59 getstatic System.out : PrintStream [15] 62 ldc <String "[finally]"> [32] 64 invokevirtual PrintStream.println(String) : void [34] 67 aconst_null 68 astore 6 70 aload 6 72 invokevirtual Object.toString() : String [37] 75 pop 76 return 77 astore_1 [e] 78 getstatic System.out : PrintStream [15] 81 ldc <String "[e]"> [43] 83 invokevirtual PrintStream.print(String) : void [23] 86 iconst_0 87 istore 5 [var] 89 getstatic System.out : PrintStream [15] 92 ldc <String "[finally]"> [32] 94 invokevirtual PrintStream.println(String) : void [34] 97 aconst_null 98 astore 6 100 aload 6 102 invokevirtual Object.toString() : String [37] 105 pop 106 return 107 astore 4 109 iconst_0 110 istore 5 [var] 112 getstatic System.out : PrintStream [15] 115 ldc <String "[finally]"> [32] 117 invokevirtual PrintStream.println(String) : void [34] 120 aconst_null 121 astore 6 123 aload 6 125 invokevirtual Object.toString() : String [37] 128 pop 129 aload 4 131 athrow Exception Table: [pc: 4, pc: 40] -> 40 when : java.lang.NullPointerException [pc: 0, pc: 19] -> 77 when : java.lang.Exception [pc: 39, pc: 56] -> 77 when : java.lang.Exception [pc: 76, pc: 77] -> 77 when : java.lang.Exception [pc: 0, pc: 19] -> 107 when : any [pc: 40, pc: 56] -> 107 when : any [pc: 77, pc: 86] -> 107 when : any
Added GenericTypeTest#test045
Now consider all surrounding try & synchronized statements in subroutines collections, and properly interact with their exception handlers (enter/exit). Fixed
Verified for 3.2 M6 using build I20060327-0010