English 中文(简体)
适当处理例外
原标题:Handling an exception properly

I am not good at handling exceptions, so I need a hint at this occasion: I want to put arrays in a collection (ArrayList) all of which should be of the same length. Otherwise errors emerge in computations. When an array of not desired length is to be inserted in the ArrayList, I would like to throw an exception with a message. What kind of exception is suitable for this occasion?

我担心的是,我必须检查将要加插的阵列的规模(如果说的话)。 是否有理由在审判中作陈述?

这里是相关的碎片代码:

投入数据:阵列

阵列:第一阵列规定的阵列长度

有些人能否修改尝试——追赶障碍?

public void insertData(double[] arraydata){
    if(this.inputdata.isEmpty()){
        inputdata.add(arraydata);
        this.arraylength = arraydata.length; 
    }else{
        try {
           if(this.arraylength == arraydata.length)
               inputdata.add(arraydata);
        }catch(Exception exception){
            System.err.printf("Missmatch array dimensions in %d place",inputdata.size()+1);           
        }
    }
}
最佳回答

例外情况只应适用于例外情况。 如果这种情况经常发生,你可能想以另一种方式处理,在工作流程中采用标准逻辑。 例如,您可重新编造true。 如果插入的阵列不是正确的长度,请插入数据,并插入<代码>false。 或者在用户进入阵列值时,你可以检查,然后告诉他们,时间长度必须是<代码>x<>。

如果这确实是一个例外的情况,将,Illegal Argument Exception,如Illegal Argument Exception。

if(this.arraylength == arraydata.length)
    inputdata.add(arraydata);
} else {
    throw new IllegalArgumentException("Ever array needs same length...");
}

类似情况。

作为书面材料,您的代码现在正在追捕add运行中的任何例外。 如我的例子所示,你不应在<条码>中放弃一种例外情况。 例外情况应当放在数据方法之外。 这意味着你不需要在<条码>内填入<<0>>>的试捕书。

还注意到是一个平时例外,因此,如果你不希望的话,就不必予以推翻或抓捕。 如果你想要的话,你仍然可以控制。

问题回答

你正在做些什么

    try {
       if(this.arraylength == arraydata.length)
           inputdata.add(arraydata);
    }catch(Exception exception){
        System.err.printf("Missmatch array dimensions in %d place",inputdata.size()+1);           
    }

∗∗∗∗∗ 但inputdata.add不会有任何例外。 相反,你应当有一个例外,让打电话者知道什么是错的:

   if(this.arraylength != arraydata.length)
       throw new IllegalArgumentException("Array length " + arraydata.length 
           + " is not same as previous length " + this.arraylength);
   inputdata.add(arraydata);

例外情况包括发出有用的信息,让打电话者知道什么是错的。

请注意,我已经推翻了这一检验,如果下一代不能接受,那么例外就被推翻了;否则,执行将持续到下行。

什么例外是适当的? 依靠。 这里需要考虑一些问题。

  1. Is this code part of a method, and was it the intent that the user have the responsibility to pass an array of the right size? The caller made a mistake, so use an IllegalArgumentException.
  2. Is this code part of a larger line of code, and was it the intent that the code would have constructed correctly sized arrays? One of your assumptions is wrong, so use an IllegalStateException.
  3. The situation of wrong-sized arrays is legitimate in some way, and your handler will repair the situation at some level, then continue. I would roll my own Exception for this case.

你的法典似乎就是第(1)条。 所提供的答案显示了处理该问题的一些良好方式。 我愿使用。 Guava Preconditions本人,因为他们是自证的,而且不太容易发生错误(如果说只是要根据需要维护的错误条件来指示执行——在感觉上,只增加新的支票:

import static com.google.common.base.Preconditions.checkArgument;

<snip>

public void insertData(double[] arraydata) {
    checkArgument(this.arraylength == arraydata.length,
            "Ever array needs same length...");
    inputdata.add(arraydata);
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签