【问题标题】:Error Handling in Java TaskJava 任务中的错误处理
【发布时间】:2015-08-21 18:19:14
【问题描述】:
public class SomeClass {
    int[] table;    
    int size;

    public SomeClass(int size) {
        this.size = size;    
        table = new int[size];  
    }

    public static void main(String[] args) {
        int[] sizes = {5, 3, -2, 2, 6, -4};
        SomeClass testInst;

        for (int i = 0; i < 6; i++) {
            testInst = new SomeClass(sizes[i]);
            System.out.println("New example size " + testInst.size);
        }   
    }    
}

当使用参数-2调用构造函数SomeClass时,会产生运行时错误:NegativeArraySizeException。

我希望修改此代码,以便通过使用 try、catch 和 throw 使其行为更加稳健。构造函数应该抛出一个异常,否则在使用非正参数调用时什么都不做。 main 方法应捕获异常并打印警告消息,然后在循环的所有六次迭代中继续执行。
有人指出我正确的方向吗?

【问题讨论】:

    标签: java error-handling


    【解决方案1】:

    每当你得到一个负数并在你的主要方法。 你的代码应该是这样的;

    public class SomeClass
    {
        int[] table;
        int size;
    
        public SomeClass(int size)
        {
            if ( size < 0 )
            {
                throw new IllegalArgumentException("Negative numbers not allowed");
            }
            this.size = size;
            table = new int[size];
        }
    
        public static void main(String[] args)
        {
            int[] sizes = { 5, 3, -2, 2, 6, -4 };
            SomeClass testInst;
            for (int i = 0; i < 6; i++)
            {
                try
                {
                    testInst = new SomeClass(sizes[i]);
    
                    System.out.println("New example size " + testInst.size);
                }
                catch (IllegalArgumentException e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      有一些操作,比如使用 Socket 连接到远程服务器,可能会抛出 SocketException 之类的异常。您需要捕获并处理这些异常,因为在您尝试连接之前,无法知道它是否会成功。

      NegativeArrayException 不是这样的。在尝试创建数组之前,您可以确定如果使用负大小,它将失败。
      如果大小来自您的代码,就像这种情况一样,您应该修复您的代码,而不是捕获该异常。
      如果它来自任何输入,您应该在尝试创建数组之前验证该输入并采取相应的行动。

      假设您的大小数组实际上是一个简化的输入示例,处理它的方式是:

      public class SomeClass {
      
          int[] table;
          int size;
      
          public SomeClass(int size) {
              this.size = size;
              table = new int[size];
          }
      
          public static void main(String[] args) {
              int[] sizes = {5, 3, -2, 2, 6, -4};
              SomeClass testInst;
      
              for (int i = 0; i < 6; i++) {
                  if (sizes[i] < 0) {
                      System.out.println("Warning");
                  } else {
                      testInst = new SomeClass(sizes[i]);
                      System.out.println("New example size " + testInst.size);
                  }
              }
          }
      }
      

      作为输出:

      New example size 5
      New example size 3
      Warning
      New example size 2
      New example size 6
      Warning
      

      【讨论】:

      • 非常感谢 :),这确实回答了我的问题 :)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2012-02-07
      相关资源
      最近更新 更多