【问题标题】:Setting ArrayIndexOutOfBound exception for array length为数组长度设置 ArrayIndexOutOfBound 异常
【发布时间】:2015-06-29 05:10:33
【问题描述】:

有没有办法我可以设置一个 try 并捕获 ArrayIndexOutOfBound 异常来检查数组大小是否大于 3? 例如,如果参数数组 args[] 包含 3 个以上的值,那么我希望异常显示错误。

到目前为止,这是我的代码:

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

【问题讨论】:

  • 这感觉就像XY problem。你能给出一个场景来描述你想要做什么以及为什么?
  • args[] 通过命令行参数获取其值。如果为 args 输入的值超过 3 个,或者数组的长度超过 3 个,那么它应该抛出一个 ArrayIndexOutOfBound 异常,否则它应该打印数组内容。
  • 我明白你在说你想让你的程序做什么。我不明白的是你为什么要这样做。从main() 中抛出异常是没有意义的,因为那里没有代码可以捕获它。你为什么不(例如)检查数组的大小,如果它太长,打印错误消息,然后返回?
  • 我也不太确定,这是过去的考试题,我只是想弄清楚如何让它以这种方式工作。

标签: java arrays exception


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    是的,可以抛出异常,但我必须问为什么?数组的长度属性会给你你想要的

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

    【讨论】:

    • 如果数组大小小于 3,我会尝试打印数组的内容,但如果数组大小大于 3,那么我希望它抛出异常错误并结束。跨度>
    【解决方案3】:

    你到底想做什么?它可能有助于为您提供正确的方法。根据您的要求,您上面的代码可能无法正常工作——您要求的是,如果它确实有超过 3 个,它应该抛出一个异常。但是,如果它确实超过 3 个,则分配将起作用并且不会抛出异常。

    例如想象你传入的字符串数组是:

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

    所以当你打电话时

    arg[4]= "four";
    

    您的调用将成功,新的参数将导致:

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

    它将大于您正在寻找的 3,但不会抛出异常。在这种情况下,确实没有办法抛出异常并让您捕获,因为在技术上没有任何问题。如果您试图识别这种情况并出于某种原因将其抛出,您可以执行以下操作:

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

    【讨论】:

      【解决方案4】:

      根据您在@copeg 答案中的评论, 你可以这样做:

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

      【讨论】:

      【解决方案5】:

      要抛出异常,您只需这样做:

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

      【讨论】:

        【解决方案6】:

        你可以这样做:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-15
          • 2013-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-15
          相关资源
          最近更新 更多