代码之家  ›  专栏  ›  技术社区  ›  kloop

如何在java中获取抛出IOException的文件名?

  •  0
  • kloop  · 技术社区  · 11 年前

    我有一个try块,它处理相当多的文件打开/关闭/读取/写入(不止一个文件)。

    它看起来像:

    try {
        // commands
    }
    catch (IOException e) {
        System.err.println("Error: " + e);
    }
    

    主要的问题是,如果出现错误,e.toString()不包含引发异常的文件名的信息。

    我可以分别检查每个读/写/打开/关闭操作,然后知道错误发生在哪个文件上,但这似乎违背了使用优雅的try-catch结构的目的。

    还有其他出路吗?我只想能够打印try块中出现错误的文件名。

    编辑:也许我应该说清楚这个问题是在什么情况下产生的。当我解析命令行参数、输入/输出文件等时,就会发生这种情况。我将文件名传递给可能返回IO错误的各种函数和方法。要求我通过打印错误和出现错误的文件名来处理任何文件问题似乎是合理的。我知道IOException比用文件处理IO更广泛,但考虑到IOException是一个特殊的异常类,使用一个方法来返回发生错误的IO源是有意义的。

    2 回复  |  直到 8 年前
        1
  •  2
  •   jdphenix    11 年前

    你不知道, IOException 没有有关 File 生成异常的。目的太笼统了。如果您自己滚动,则可以捕获相关信息并捕获自己的异常。

    在处理输入的各种方法中,将相关部分包装在try/catch块中,捕获 IO异常 ,并将自己的数据与其他数据一起提交。

    这是一个完整的程序,演示了基本思想。

    class FooException extends Exception { 
        private static final long serialVersionUID = 2816777468035627105L;
        private final String filename; 
        private final Throwable cause; 
    
        public FooException(String filename) { 
            this(filename, null); 
        }
    
        public FooException(String filename, Throwable cause) { 
            this.filename = filename; 
            this.cause = cause; 
        }
    
        @Override
        public String getMessage() { 
            return "Error reading file"; 
        }
    
        @Override
        public Throwable getCause() {
            return cause; 
        }
    
        public String getFilename() { 
            return filename; 
        }
    }
    
    public class Soj25375647 {    
        public static void main(String[] args) {
             try {
                 throwsAnException();
                 // Do other things that might throw my exception
             } catch (FooException e) { 
                 System.err.printf("File: %s, error: %s, caused by %s%n", e.getFilename(), e, e.getCause());
             }
        }
    
        public static void throwsAnException() throws FooException { 
            try { 
                int x = 2 / 0;
            } catch (ArithmeticException e) { 
                throw new FooException("bob.file", e);   
            }
        }
    }
    

    输出

    File: bob.file, error: soj25375647.FooException: Error reading file, caused by java.lang.ArithmeticException: / by zero
    

    另请参见 Exception-Handling Antipatterns .

        2
  •  0
  •   Ky -    8 年前

    我想我看到这里发生了什么。你可能有这样的想法:

    try {
        for (int i = 0; i < something; i++) {
            File f = getSomeFile(i);
            // Operations that might throw an IOException
        }
    }
    catch (IOException e) {
        // handle
    }
    

    这不是一个好主意;正如您所说,您不知道导致错误的文件。相反,请尝试以下操作:

    for (int i = 0; i < something; i++) {
        File f = getSomeFile(i);
        try {
            // Operations that might throw an IOException
        }
        catch (IOException e) {
            // handle
            break;
        }
    }
    

    这样,你还有 f 当抛出错误时,它也会像原始代码一样,在出现错误时跳出循环。希望这有帮助!