9

I have a method that sometimes throws an exception:

this.items[index] = element;

And I have a unit test that asserts that the exception that ought to be thrown is actually thrown:

try
{
    doSomethingWithIndex(-1);
    Assert.fail("should cause exception");
}
catch (IndexOutOfBoundsException expected)
{
    Assert.assertNotNull(expected.getMessage());
}

This test runs as part of the continuous build and sometimes, occasionally it fails because getMessage() in fact returns null. Why would this happen? My code can never throw an exception with a null message.

EDIT

My original code example was misleading, the thrown exception is actually coming from directly indexing an array. I can reproduce the same behavior with a custom thrown exception though.

I added the suggested code:

catch (IndexOutOfBoundsException expected)
{
    if (expected.getMessage() == null)
    {
        expected.printStackTrace();
    }
    Assert.assertNotNull(expected.getMessage());
}

The console output is missing the stack trace in addition to the cause. Here's the full output:

java.lang.ArrayIndexOutOfBoundsException
Craig P. Motlin
  • 26,452
  • 17
  • 99
  • 126
  • 3
    Are you sure it's not an IndexOutOfBoundsException thrown by something else? I suggest you dump the stack trace of `expected` when you come across this. – Jon Skeet Jun 30 '11 at 17:39
  • I second Jon's suggestion. Don't forget that you are probably catching either an ArrayIndexOutOfBoundsException or a StringIndexOutOfBoundsException. – Ted Hopp Jun 30 '11 at 17:48
  • @Ted,@Jon: aren't the default StringIndexOutOfBoundException and ArrayIndexOutOfBoundException generate a msg when they are thrown? (the element you were trying to access)? – amit Jun 30 '11 at 17:50
  • @amit: Yes, but it looks like it's *something* throwing ArrayIndexOutOfBoundsException... – Jon Skeet Jun 30 '11 at 18:01
  • I'm not sure if this is just sample code to illustrate a point, but does your unit test actually assert what the message of the exception is? Why? – matt b Jun 30 '11 at 18:45

3 Answers3

9

Found the answer on a similar question.

The JIT compiler will optimize away stack traces in certain exceptions if they happen enough.

The JVM flag -XX:-OmitStackTraceInFastThrow prevents this behavior and seems to fix the flickering unit tests.

Community
  • 1
  • 1
Craig P. Motlin
  • 26,452
  • 17
  • 99
  • 126
1

Try printing stack trace of exception when it has null message. It's possible that some other code throws it. Like you actually accessing past array length.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
0

You wrote that: "My code can never throw an exception with a null message"

Are you yousing any 3rd party library? I assume standard java codes never throw exceptions like this above, but some pourly coded jar... :)

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
  • No 3rd party libraries. I see this "flickering" behavior for both my own thrown exceptions and referencing an array directly with a -1 index. – Craig P. Motlin Jun 30 '11 at 17:55