h1

Try catch finally wierdness in Groovy 1.6

12 September, 2010

Had a fun time for a few hours this week!

I was doing some grails work for client, carrying on the webdriver integration. Using the grails webdriver plugin. All seems well, except that I was seeing a difference in the behaviour of my tests between the IDE and when invoked from the command line.
Using the maven grails integration, I ended up with groovy 1.6.something, standard for Grails 1.2.2.

When running the webdriver tests (junit) in eclipse, everything proceeds as normal, errors are reported normally, windows are cleaned up. When run from a maven command, any test that is in error will leave a firefox window behind.

As you might imagine, this is a bit of a nightmare for the CI server.

I eventually tracked it to a difference in behaviour between groovy 1.6 and 1.7.

Eclipse had picked up 1.7 to run against, as thats what the eclipse groovy plugin gave me. Maven carried on with 1.6

Given this groovy code (exists in the Grails WebDriver plugin, WebDriverTestCase.groovy btw) :-


try {
prinltn "Trying..."
} catch (Throwable t) {
println "Caught ..."
throw t
} finally {
println "Finally!"
}
println "End"

Would you believe that in groovy 1.6 the log would read :-

Trying…
Caught…
End

Whereas in 1.7

Trying…
Caught…
Finally!
End

Groovy 1.6 will miss the finally block when a throwable is rethrown from inside a catch block.

The workaround is to either upgrade, or extract the finally to surround the try/ catch. This is the approach I took.


try {
try {
prinltn "Trying..."
} catch (Throwable t) {
println "Caught ..."
throw t
}
} finally {
println "Finally!"
}
println "End"

Both versions now exhibit the same behaviour.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.