0

在我的方法中,在下面的伪代码中,在一行中创建了两个异常(ArrayIndexOutOfBounds 和 NumberFormatException)。

在这一行上,输入文件从 X+Y 列更改为 X 列。

外部 catch 首先被捕获并在内部 catch 自己完成之前停止程序。

我需要内部循环来捕获异常,因为我正在使用它将数据打印到文件中。

try{
    Initialize local variables
    //Includes getting numerical strings from input fields
    //Hence NumberFormatException to check for correct input
    try{
        get file name
        //hence FileNotFoundException
        try{
            read line, get column A, B and C 
            //Column B is at an index larger than X
            //hence ArrayIndexOutOfBoundsException
        }catch(ArrayIndexOutOfBoundsException){
            print data to file
        }
    } catch(FileNotFoundException){
        error message
    }
} catch(NumberFormatException){
    error message
    //error caught here, checks for whether
    //numbers were entered in the required fields
}

有没有办法强制内部捕获首先触发,或者代码是否需要重新处理?

4

2 回答 2

4

尝试在内部块中捕获更一般的异常。如果捕获的异常是 instanceof NumberFormatException,则将其传递给外部块或在此处返回错误消息。

于 2013-08-27T09:53:24.963 回答
0

问题是正在读取的 csv 文件有额外的逗号来阻止文件被锯齿状;我依靠打印到文件的想法。

该异常对于预期的输入是正确的,但现在我正在捕获NumberFormatExceptionIndexOutOfBoundsException在最内部的捕获中使用 multicatch 来获得这种额外的可能性。

于 2013-08-27T10:16:44.360 回答