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

  • Home
  • SEARCH
  • 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 9202627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:18:04+00:00 2026-06-17T23:18:04+00:00

new to StackOverFlow and fairly new to Java. Been programming in C prior to

  • 0

new to StackOverFlow and fairly new to Java. Been programming in C prior to this and am trying to get the foundations of Java.

Just a bit confused about the following code:

public class Exercise5 {
private static int[] ia = new int[3];
static int x = 5;   
public static void main(String[] args) {
    while(true) {
        try {
            ia[x] = 1;
            System.out.println(ia[x]);
            break;  
        } catch(ArrayIndexOutOfBoundsException e) {
            System.err.println(
                "Caught ArrayIndexOutOfBoundsException");
                e.printStackTrace();
            x--;
        } finally {
            System.out.println("Are we done yet?");     
        }
    }
    System.out.println("Now, we're done.");
}   

}

I am still trying to get my head around the try,catch and finally blocks. What I do not understand is in this code the program only runs until the first instance that a non exception occurs happens and then it exits the while loop.

My understanding is that the while loop will run until you run out of memory so can someone please explain how the code exits the while loop on the first instance of non exception.

Thank you!

Marco

  • 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-06-17T23:18:06+00:00Added an answer on June 17, 2026 at 11:18 pm

    First, x is equals to 5 and then program goes to the while loop. Since 5 is greater than the size of the array, it will give exception and go to the exception block. Here, x will be decreased and run into the while loop again. Until x reaches to 2, there is no exception anymore; therefore, it can reach the break line. So, program can exit the while loop and finish

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

Sidebar

Related Questions

I'm fairly new to programming but I've been reading some interesting discussions on StackOverflow
I am new to IntelliJ (and Stackoverflow) and fairly new to Java,In my application
I'm fairly new to Objective-C, and this is my first question on StackOverflow. Please
I'm fairly new to Java (and stackoverflow for that matter) so I'll try my
This is my first question here on StackOverflow, and quite frankly I'm fairly new
I am fairly new to Java and in another Stack Overflow question about for
I'm fairly new to SQL server (this is my second post on StackOverflow). I'm
This is a fairly recurring theme on StackOverflow, but once again I can't get
I am fairly new to programming and while doing a lot of reading this
I'm playing a little bit with the new StackOverflow API . Unfortunately, my JSON

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.