Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6082425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:17:16+00:00 2026-05-23T11:17:16+00:00

I have a method that sometimes throws an exception: this.items[index] = element; And I

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:17:16+00:00Added an answer on May 23, 2026 at 11:17 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an each method that is run on some user-submitted data. Sometimes it
I have class method that returns a list of employees that I can iterate
I have a method that where I want to redirect the user back to
I have a method that can return either a single object or a collection
I have a method that takes an IQueryable. Is there a LINQ query that
I have a method that periodically (e.g. once in every 10 secs) try to
I have a method that's about ten lines of code. I want to create
I have a method that needs to accept an array of country names, and
I have a method that creates a MessageDigest (a hash) from a file, and
I have a method that, given an angle for North and an angle for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.