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 9160829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:49:48+00:00 2026-06-17T13:49:48+00:00

Which one should be preferred over the other? Example: class Example { int[] A

  • 0

Which one should be preferred over the other? Example:

class Example {
    int[] A = new A[5];

    void setArray(int item, int index) {
        if(index < 0 || index >= 5) {
            System.out.println("Index out of bounds");
        } else {
            A[index] = item;
        }
    }
}

or this one?

class Example {
    int[] A = new A[5];

    void setArray(int item, int index) {
        try {
            A[index] = item;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Index out of bounds");
        }
    }
}
  • 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-17T13:49:49+00:00Added an answer on June 17, 2026 at 1:49 pm

    When a language has good support for exceptions (like C# and Java do), throwing and catching exceptions is the most common way of handling exceptional cases.

    The reason is that it makes the logic of the code clearer: methods can be written for the general case (without bothering to resolve error codes and such) and only the few methods that actually have enough context to handle the unusual case will also contain the code to do so.

    Your example doesn’t demonstrate what I’m saying because you’ve caught the exception at the same level where it happened. In practice, exceptions are useful because they are thrown a few levels above, some of them even reaching near the top level of the program, aborting all the methods that were going to be called by that level afterwards.

    The following explanation will hopefully shed some light on the importance of exceptions, when they are used properly:

    First off, consider that an error message saying array index out of bounds is meaningless to the user. Even simply displaying another kind of error message, such as one saying the file being read has an incorrect format is much more useful, even if it was deduced (by your program) from the fact that an array was accessed out of bounds. But you can’t just change the error message in the set method of the array, because it wouldn’t make any sense – an array is used for many things and an out of bounds indexing attempt can mean many different things (depending on how the flow reached there). Only a method that’s high enough in the calling hierarchy knows what each kind of exception actually means.

    Second, letting the exception travel a few levels higher allows your program to reason better about the problem and possibly try another course of action, possibly without bothering the user at all.

    Third, catching the exception and displaying an error message at such a low level doesn’t let the caller method know that an exception has occurred at all. This means that the higher level logic of the program will continue its normal flow as if nothing had happened, thinking that every operation until now has been successful. This is a kind of wrong behavior and it can range from relatively harmless to destructive.

    So, to answer your question, usually the correct (practical) approach for the specific example in your question would be simply

    void setArray(int item, int index) {
        A[index] = item;
    }
    

    and you’d have to handle the exception at a higher level. If you want a general way of deciding what to do in those cases, read my answer here.

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

Sidebar

Related Questions

Which one of the following two pieces of code should be preferred over other?
Which is preferred/right or when should you use one over the other and also
Which of these patterns should be preferred over the other and why? foreach(KeyValuePair<string, Dictionary<string,
<?python class += 1 ?> One really simple line of code which definitely should
Which one out of the following two should be preferred while doing && operation
What's the difference? Which is preferred, or when should I use each one respectively?
which one should be used to manipulating data, Array or Array Object? Like search,sort
I'm wondering given both the native js and jquery event handler, which one should
I was wondering in which cases one should put the values of an animation
I'm a little confused as to which one I should use. I think either

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.