2

Is there a way i can set a try and catch ArrayIndexOutOfBound exception to check if the array size is greater than 3? For example if the parameter array args[] holds more than 3 values, then i want the exception to show an error.

Here is my code so far:

public static void main (String[] args){
try {
    args[4] = "four";
    } catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Array index out of bounds.");
    }
}
4
  • This feels like an XY problem. Can you give a scenario which describes what you are trying to do and why? Commented Apr 21, 2015 at 20:28
  • 1
    args[] gets its value through command line argument. If one enters more than 3 values for args or the array exceeds by more than 3 in length then it should throw an ArrayIndexOutOfBound exception, otherwise it should print the array contents. Commented Apr 21, 2015 at 20:36
  • I understand what you are saying you want your program to do. What I don't understand is why you want it to do that. Throwing an exception out of main() is pointless, since there's no code there to catch it. Why aren't you (for instance), checking the size of the array, printing an error message if it's too long, and then returning? Commented Apr 21, 2015 at 20:38
  • Im not quite sure either, it was a past exam question and i'm just trying to figure out how to make it work this way. Commented Apr 21, 2015 at 20:40

6 Answers 6

4

You can use args.length to get the number of elements in the array.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes it is possible to throw an exception, but I must ask why? The length property of an array will give you what you want

if ( args.length > 3 ){
   //do something here
}

1 Comment

I am trying to print the contents of the array if the array size is less than 3, but if the array size is greater than 3 then i want it to throw an exception error and end.
2

What exactly are you trying to do? It may help to provide you with the correct approach. Your above code may not work based on what you are asking -- you are asking that if it does have more than 3, it should throw an exception. However in the case that it does have more than 3, the assignment would work and there would be no exception thrown.

For example imagine the string array you are passing in is:

args = ['arg1','arg2','arg3','arg4,'arg5']

thus when you make the call

arg[4]= "four";

your call would be successful and the new args would result in:

args = ['arg1','arg2','arg3','arg4,'four'];

It would be greater than 3 which is what you are looking for, but no Exception would be thrown. There really is no way for an exception to be thrown and for you to catch in this case, since there is nothing technically wrong. If you are trying to identify this situation and throw it for some reason, you could do the following:

public static void main (String[] args){
var maxAllowedSize = 3;
try {
    args[maxAllowedSize] = "four";
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Array index out of bounds.");
    }

if (args.length > maxAllowedSize) {
  System.out.println("the length is too large " + args.length);
  throw new Exception(); //better to make a specific Exception here
  }
}

Comments

1

From your comment in @copeg answer, you can do:

        if ( args.length < 3 ){ 
        System.out.println("There I print my content, size is less than 3");
        //print your content 
        } 
        else if( args.length == 3 ) { //You didnt point out what you want to do if size is equal to 3
        System.out.println("Size is equal to 3");
        }
        else if( args.length > 3 ) {
         throw new ArrayIndexOutOfBoundsException("Out of Bounds Exc. Size is 4 or more"); 
        }

1 Comment

Wouldn't SizeLimitExceededException be more appropriate?
1

To throw an Exception you simply do:

if (args.length > 3) {
   throw new AnyExceptionYouWant();
}

Comments

1

You can do something like this:

if ( args.length > 3 ){
    throws new ArrayIndexOutOfBoundsException("Wrong array size! (actual: " + args.length + ", max: 3)");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.