Java打印出在不同位置抛出的异常和未抛出的异常

Java打印出在不同位置抛出的异常和未抛出的异常,java,Java,我不知道如何制定标题,但我有以下问题。我正在制作一个程序,从CSV文件中读取内容并将其键入。在一个没有问题的文件中,我通常会得到结果。当我运行另一个抛出错误的文件时,我的程序仍然打印出catch中的内容,但不是按照我希望的顺序打印出来的-我希望将所有抛出错误的部分放在打印的顶部,而将没有错误的部分放在底部。我该怎么做? 这是我目前拥有的代码 String []arrayS = line.split(","); if(arrayS.length==7){

我不知道如何制定标题,但我有以下问题。我正在制作一个程序,从CSV文件中读取内容并将其键入。在一个没有问题的文件中,我通常会得到结果。当我运行另一个抛出错误的文件时,我的程序仍然打印出catch中的内容,但不是按照我希望的顺序打印出来的-我希望将所有抛出错误的部分放在打印的顶部,而将没有错误的部分放在底部。我该怎么做? 这是我目前拥有的代码

           String []arrayS = line.split(",");
           if(arrayS.length==7){
              String fName = "";
              String lName = "";
                             //String 
              int bDay;
              int bMonth;
              int bYear;
              int weight;
              double height;
              try{
                 //System.out.printf("%15s%15s%15s%15s \n", fnlName , birthDate, sWeight, sHeight);

                 fName = arrayS[0];//gets first line in csv- name
                 lName = arrayS[1];//gets second line in csv- last name
                 try{
                 bDay = Integer.parseInt(arrayS[2].trim());//gets third line in csv- birth day
                 }catch (NumberFormatException nfe){
                    System.err.println("Error found in" + arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2]  + ", " + arrayS[3]  + ", " + arrayS[4]  + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Birth day");
                    continue;
                 }try{
                 bMonth = Integer.parseInt(arrayS[3].trim());//gets four line in csv- birth month
                 }catch (NumberFormatException nfe){
                    System.err.println("Error found in" + arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2]  + ", " + arrayS[3]  + ", " + arrayS[4]  + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Birth month");
                    continue;
                 }try{
                 bYear = Integer.parseInt(arrayS[4].trim());//gets fifth line in csv- birth year
                 }catch (NumberFormatException nfe){
                    System.err.println("Error found in" + arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2]  + ", " + arrayS[3]  + ", " + arrayS[4]  + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Birth year");
                    continue;
                 }try{
                 weight = Integer.parseInt( arrayS[5].trim());//gets sixth line in csv- weight
                 }catch (NumberFormatException nfe){
                    System.err.println("Error found in" +arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2]  + ", " + arrayS[3]  + ", " + arrayS[4]  + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Weight");
                    continue;
                 }try{
                 height = Double.parseDouble(arrayS[6].trim());//gets seventh line in csv- height
                 }catch (NumberFormatException nfe){
                    System.err.println("Error found in" + arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2]  + ", " + arrayS[3]  + ", " + arrayS[4]  + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Height");
                    continue;
                 }
                 System.out.printf("%15s%15s%02d/%02d/%4d %d %.1f\n" , fName , lName  , bDay , bMonth , bYear , weight , height);

              }catch(NumberFormatException nfe ){
                 System.out.println("Cannot read student:" +  fName);
                 continue;
              }}```

不要将输出直接写入
System.out
System.err
,而是首先收集它们。您可以使用不同的方法,如使用
列表
或使用
StringBuilder
。当您完全读取CSV文件后,您最终可以输出最后收集的结果,首先是错误,然后是无错误条目。代码可以如下所示:

List<String> errors = new ArrayList<String>();
List<String> output = new ArrayList<String>();

while (/*reading a line */) {
    /* [...] */

    try {
        /* [...] */
        output.add(String.format("...", a, b, c, d, ...);
    } catch (...) {
        errors.add(...);
    }
}

// now show the result, but show the errors first
if (!errors.isEmpty()) {
    System.err.println("There were errors:");
    for (String str: errors) {
        System.err.println(str);
    }
}
System.out.println("Your result");
for (String str: output) {
    System.out.println(str);
}
List errors=new ArrayList();
列表输出=新的ArrayList();
当(/*读一行*/){
/* [...] */
试一试{
/* [...] */
add(String.format(“…”,a,b,c,d,…);
}捕获(…){
错误。添加(…);
}
}
//现在显示结果,但首先显示错误
如果(!errors.isEmpty()){
System.err.println(“有错误:”);
for(字符串str:errors){
系统错误打印项次(str);
}
}
System.out.println(“您的结果”);
for(字符串str:output){
系统输出打印项次(str);
}