Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何改变这一点,使它以我想要的方式失败?_Java_File_Csv_Error Handling_Opencsv - Fatal编程技术网

Java 我如何改变这一点,使它以我想要的方式失败?

Java 我如何改变这一点,使它以我想要的方式失败?,java,file,csv,error-handling,opencsv,Java,File,Csv,Error Handling,Opencsv,我认为如果遇到任何错误,下面的代码就会停止,句号 public static void exportDatapointsToCSV(List<Datapoint> datapointList, File exportDir) { SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy (h:mm a)", Locale.US); File file = new File(exportDir, "my_dat

我认为如果遇到任何错误,下面的代码就会停止,句号

public static void exportDatapointsToCSV(List<Datapoint> datapointList, File exportDir) {

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy (h:mm a)", Locale.US);
    File file = new File(exportDir, "my_datapoints.csv");

    try {
        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
        csvWrite.writeNext(new String[]{"DATAPOINT_LABEL", "VALUE", "TIMESTAMP", "TIMESTAMP_FORMATTED", "NOTE"});

        for (Datapoint datapoint : datapointList) {
            String arrStr[] = new String[]{
                    datapoint.getLabel(),
                    datapoint.getValue() + "",
                    datapoint.getTimestamp() + "",
                    sdf.format(new Date(datapoint.getTimestamp())) + "",
                    datapoint.getNote()
            };
            csvWrite.writeNext(arrStr);
        }
        csvWrite.close();
    }
    catch (Exception sqlEx) {
        Log.e(LOG_TAG, sqlEx.getMessage(), sqlEx);
    }
}
public static void exportDatapointsToCSV(列表datapointList,文件exportDir){
SimpleDataFormat sdf=新的SimpleDataFormat(“mm-dd,yyyy(h:mm-a)”,Locale.US);
File File=新文件(exportDir,“my_datapoints.csv”);
试一试{
createNewFile();
CSVWriter csvWrite=新的CSVWriter(新的文件编写器(文件));
writeNext(新字符串[]{“数据点标签”、“值”、“时间戳”、“时间戳格式化”、“注释”});
用于(数据点数据点:数据点列表){
字符串arrStr[]=新字符串[]{
datapoint.getLabel(),
datapoint.getValue()+“”,
datapoint.getTimestamp()+“”,
格式(新日期(datapoint.getTimestamp())+“”,
datapoint.getNote()
};
csvWrite.writeNext(arrStr);
}
csvWrite.close();
}
catch(异常sqlEx){
Log.e(Log_标记,sqlEx.getMessage(),sqlEx);
}
}
但我希望它:

  • 如果无法创建要开始的文件,则抛出错误并停止

  • 如果它在给定行上遇到错误,则记录它,但移动到下一行,而不是停止整个操作


  • 我是否只是将try/catch移动到循环中?这包括它吗?

    您捕获的异常是所有异常的祖父-如果您查看,您将看到构造函数
    新FileWriter(文件)
    抛出一个
    IOException

    因此,如果您希望执行某些操作,例如记录日志并在失败时退出,请尝试

     CSVWriter csvWrite = null;
     try {
             csvWrite = new CSVWriter(new FileWriter(file));
     } catch (IOException ex) {
        Log.e(LOG_TAG, ex.getMessage(), ex);
        return; // or System.exit or what either
     }
    

    如果我在代码中正确显示:

    public static void exportDatapointsToCSV(List<Datapoint> datapointList, File exportDir) {
    
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy (h:mm a)", Locale.US);
    
        File file = null;
        try{ ( file = new File(exportDir, "my_datapoints.csv") ).createNewFile(); }
        catch(IOException e){throw new RuntimeException(e); }
    
        CSVWriter csvWrite = null;
        try {
            csvWrite = new CSVWriter(new FileWriter(file));
            csvWrite.writeNext(new String[]{"DATAPOINT_LABEL", "VALUE", "TIMESTAMP", "TIMESTAMP_FORMATTED", "NOTE"});
    
            for (Datapoint datapoint : datapointList) {
                String arrStr[] = new String[]{
                        datapoint.getLabel(),
                        datapoint.getValue() + "",
                        datapoint.getTimestamp() + "",
                        sdf.format(new Date(datapoint.getTimestamp())) + "",
                        datapoint.getNote()
                };
                try{ csvWrite.writeNext(arrStr); }
                catch(Exception e){LOG.error(e);}
            }
        }
        catch (Exception sqlEx) {
            Log.e(LOG_TAG, sqlEx.getMessage(), sqlEx);
        }finally{
           if (csvWrite != null){
               csvWrite.close();
           }
        }
    }
    
    public static void exportDatapointsToCSV(列表datapointList,文件exportDir){
    SimpleDataFormat sdf=新的SimpleDataFormat(“mm-dd,yyyy(h:mm-a)”,Locale.US);
    File=null;
    尝试{(file=newfile(exportDir,“my_datapoints.csv”)).createNewFile();}
    catch(IOException e){抛出新的RuntimeException(e);}
    CSVWriter csvWrite=null;
    试一试{
    csvWrite=新的CSVWriter(新的文件编写器(文件));
    writeNext(新字符串[]{“数据点标签”、“值”、“时间戳”、“时间戳格式化”、“注释”});
    用于(数据点数据点:数据点列表){
    字符串arrStr[]=新字符串[]{
    datapoint.getLabel(),
    datapoint.getValue()+“”,
    datapoint.getTimestamp()+“”,
    格式(新日期(datapoint.getTimestamp())+“”,
    datapoint.getNote()
    };
    请尝试{csvWrite.writeNext(arrStr);}
    catch(异常e){LOG.error(e);}
    }
    }
    catch(异常sqlEx){
    Log.e(Log_标记,sqlEx.getMessage(),sqlEx);
    }最后{
    if(csvWrite!=null){
    csvWrite.close();
    }
    }
    }
    
    change
    catch(Exception sqlEx){
    对特定的
    异常执行特定操作code@ScaryWombat你到底是什么意思?我不知道这些“肯定”是什么意思请参阅我的答案以获取更多帮助您不必将所有内容都打包到一个巨大的try/catch块中。您始终可以进行更精细的异常处理。将
    createNewFile
    writeNext
    打包到它们自己的try/catch块中。@tsolakp这么嵌套的try/catch块?外部块引发IOException,内部块引发异常?什么是appr在这里处理错误是否合适?将错误显示给用户,或者只是将其静默地转储到文件中?我对您的应用程序一无所知。如果您静默地转储错误,用户会有什么感觉?