简体   繁体   中英

Setting ArrayIndexOutOfBound exception for array length

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.");
    }
}

您可以使用args.length获取数组中元素的数量。

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
}

To throw an Exception you simply do:

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

You can do something like this:

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

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"); 
        }

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
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM