Home > Other > Return in finally skips the catch!

Return in finally skips the catch!

I was writing a JUnit Rule, when I realized that my test failures weren’t being passed through to the JUnit framework.

Here’s what my code looked like:

public Statement apply(final Statement base, FrameworkMethod method, Object target) {
  return new Statement() {
    @Override
    public void evaluate() throws Throwable {
      try {
        base.evaluate();
      }
      catch (Throwable t) {
        throw t;
      }
      finally {
        boolean something = false;
        // check something
        if( something ) {
          return;
        }
        // do what I should otherwise do
      }
    }
  };
}

Bizarrely (or not), that Throwable was not being thrown. After trying some different approaches, I realized that the return out of the finally was the cause.

Advertisements
Categories: Other
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: