java - Why does Throwable.getMessage() occasionally return null? -
i have method throws exception:
this.items[index] = element;
and have unit test asserts exception ought thrown thrown:
try { dosomethingwithindex(-1); assert.fail("should cause exception"); } catch (indexoutofboundsexception expected) { assert.assertnotnull(expected.getmessage()); }
this test runs part of continuous build , sometimes, fails because getmessage() in fact returns null. why happen? code can never throw exception null message.
edit
my original code example misleading, thrown exception coming directly indexing array. can reproduce same behavior custom thrown exception though.
i added suggested code:
catch (indexoutofboundsexception expected) { if (expected.getmessage() == null) { expected.printstacktrace(); } assert.assertnotnull(expected.getmessage()); }
the console output missing stack trace in addition cause. here's full output:
java.lang.arrayindexoutofboundsexception
found answer on similar question.
the jit compiler optimize away stack traces in exceptions if happen enough.
the jvm flag -xx:-omitstacktraceinfastthrow prevents behavior , seems fix flickering unit tests.
Comments
Post a Comment