Java编写程序时要考虑到所有可能的异常

要考虑到所有可能出现异常的情况,并对异常做处理操作,日志记录,不然程序会终止运行

public void downFromCloud(String inputDir, String outputDir) {
        Configuration conf = new Configuration();
        // 实例化一个文件系统
        FileSystem fs = null;
        FSDataInputStream in = null;
        Path[] paths = null;
        OutputStream outs = null;
        try {
            fs = FileSystem.get(conf);
            Path inputPath = new Path(inputDir);
            FileStatus dirStatus = fs.getFileStatus(inputPath);
            if (!dirStatus.isDir()) {
                return;
            }
            FileStatus[] filesStatus = fs.listStatus(inputPath,
                    new HadoopFileFilter(".*?\\.xml"));
            paths = FileUtil.stat2Paths(filesStatus);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            System.out.println("exception 1" + e1.getMessage());
            log4j.error("Download.class exception 1:"+e1.getMessage());
        }
        int length = paths.length;
        System.out.println("length:" + length);
        for (int i = 0; i < length; i++) {
            try {

                in = fs.open(paths[i]);
                // 将InputStrteam 中的内容通过IOUtils的copyBytes方法复制到OutToLOCAL中
                outs = new FileOutputStream(new File(outputDir
                        + paths[i].getName()));
                IOUtils.copyBytes(in, outs, 1024, true);
                in.close();
                outs.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("exception 2" + e.getMessage());
                log4j.error("Download.class exception 2"+e.getMessage());
            } finally {
                try {
                    in.close();
                    outs.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    System.out.println("exception 3" + e.getMessage());
                    log4j.error("Download.class exception 3:"+e.getMessage());
                }

            }
        }
    }

Java编写程序时要考虑到所有可能的异常,布布扣,bubuko.com

Java编写程序时要考虑到所有可能的异常

上一篇:Search in Rotated Sorted Array II leetcode java


下一篇:风雨java路之【基础篇】——异常的那些事儿