@@ -287,4 +287,185 @@ final class Groovy4721 {
287287 assert new TryFinallyJavaTest().test() == groovyResult
288288 """
289289 }
290+
291+ @Test
292+ void testAccessingVariableInTryCatchFinally_1 () {
293+ def declareClass = { String lang ->
294+ """
295+ public class TryFinally${ lang} Test {
296+ public String test() {
297+ String result = "result: ";
298+ try {
299+ String x = "foo";
300+ result += x;
301+ throw new RuntimeException("expected");
302+ } catch (RuntimeException e) {
303+ result += "caught";
304+ return result;
305+ } finally {
306+ result += "-finally";
307+ }
308+ }
309+ }
310+ """
311+ }
312+
313+ JavaShell js = new JavaShell ()
314+ final mcn = " tests.TryFinallyJavaTest"
315+ js. compile(mcn, " package tests;\n ${ declareClass('Java')} " )
316+
317+ new GroovyShell (js. getClassLoader()). evaluate """ \
318+ package tests;
319+ import tests.TryFinallyJavaTest
320+
321+ ${ declareClass('Groovy')}
322+
323+ final groovyResult = new TryFinallyGroovyTest().test()
324+ assert 'result: foocaught' == groovyResult
325+ assert new TryFinallyJavaTest().test() == groovyResult
326+ """
327+ }
328+
329+ @Test
330+ void testNestedTryCatchFinally () {
331+ assertScript ''' \
332+ class MyClass {
333+ def myMethod() {
334+ String result = "result: "
335+ try {
336+ result += "outer-try;"
337+ try {
338+ result += "inner-try;"
339+ throw new RuntimeException("inner")
340+ } catch (RuntimeException e) {
341+ result += "inner-catch;"
342+ } finally {
343+ result += "inner-finally;"
344+ }
345+ result += "after-inner;"
346+ } finally {
347+ result += "outer-finally"
348+ }
349+ return result
350+ }
351+ }
352+ assert 'result: outer-try;inner-try;inner-catch;inner-finally;after-inner;outer-finally' == new MyClass().myMethod()
353+ '''
354+ }
355+
356+ @Test
357+ void testFinallyExecutesWithBreak () {
358+ assertScript ''' \
359+ class MyClass {
360+ def myMethod() {
361+ String result = "result: "
362+ for (int i = 0; i < 3; i++) {
363+ result += i + "("
364+ try {
365+ result += "try;"
366+ if (i == 1) break
367+ result += "after-break;"
368+ } finally {
369+ result += "finally);"
370+ }
371+ }
372+ return result
373+ }
374+ }
375+ assert 'result: 0(try;after-break;finally);1(try;finally);' == new MyClass().myMethod()
376+ '''
377+ }
378+
379+ @Test
380+ void testExceptionInFinally () {
381+ def declareClass = { String lang ->
382+ """
383+ public class TryFinally${ lang} Test {
384+ public String test() {
385+ String result = "result: ";
386+ try {
387+ return (result += "from-try;");
388+ } finally {
389+ throw new RuntimeException(result += "finally-error");
390+ }
391+ }
392+ }
393+ """
394+ }
395+
396+ JavaShell js = new JavaShell ()
397+ final mcn = " tests.TryFinallyJavaTest"
398+ js. compile(mcn, " package tests;\n ${ declareClass('Java')} " )
399+
400+ new GroovyShell (js. getClassLoader()). evaluate """ \
401+ package tests;
402+ import tests.TryFinallyJavaTest
403+
404+ ${ declareClass('Groovy')}
405+
406+ final groovyErr = groovy.test.GroovyAssert.shouldFail(RuntimeException) {
407+ new TryFinallyGroovyTest().test()
408+ }
409+ final javaErr = groovy.test.GroovyAssert.shouldFail(RuntimeException) {
410+ new TryFinallyJavaTest().test()
411+ }
412+
413+ assert 'result: from-try;finally-error' == groovyErr.message
414+ assert javaErr.message == groovyErr.message
415+ """
416+ }
417+
418+ @Test
419+ void testMultipleCatchBlocks () {
420+ def declareClass = { String lang ->
421+ """
422+ public class TryFinally${ lang} Test {
423+ public String test(int type) {
424+ String result = "result: ";
425+ try {
426+ result += "try;";
427+ switch (type) {
428+ case 1: throw new IllegalArgumentException("IAE");
429+ case 2: throw new NullPointerException("NPE");
430+ case 3: throw new RuntimeException("RE");
431+ }
432+ result += "no-throw;";
433+ } catch (IllegalArgumentException e) {
434+ result += "catch-IAE;";
435+ } catch (NullPointerException e) {
436+ result += "catch-NPE;";
437+ } catch (Exception e) {
438+ result += "catch-other;";
439+ } finally {
440+ result += "finally";
441+ }
442+ return result;
443+ }
444+ }
445+ """
446+ }
447+
448+ JavaShell js = new JavaShell ()
449+ js. compile(" tests.TryFinallyJavaTest" , " package tests;\n ${ declareClass('Java')} " )
450+
451+ new GroovyShell (js. getClassLoader()). evaluate """ \
452+ package tests;
453+ import tests.TryFinallyJavaTest
454+
455+ ${ declareClass('Groovy')}
456+
457+ def groovy = new TryFinallyGroovyTest()
458+ def java = new TryFinallyJavaTest()
459+
460+ assert 'result: try;catch-IAE;finally' == groovy.test(1)
461+ assert 'result: try;catch-NPE;finally' == groovy.test(2)
462+ assert 'result: try;catch-other;finally' == groovy.test(3)
463+ assert 'result: try;no-throw;finally' == groovy.test(0)
464+
465+ assert java.test(1) == groovy.test(1)
466+ assert java.test(2) == groovy.test(2)
467+ assert java.test(3) == groovy.test(3)
468+ assert java.test(0) == groovy.test(0)
469+ """
470+ }
290471}
0 commit comments