SML:如何检查是否已引发异常?

时间:2015-11-09 14:36:50

标签: io sml

我正在编写一些关于在SML中读取和写入文件的简单程序,我正在考虑跟踪文件是否成功打开/溢出发生等。如果在complilation期间引发任何异常,我希望函数返回false,否则为true。

这样的实施是否可行?如果是这样,怎么样?如果没有,任何替代解决方案?

我不确定我是否正确地做到了这一点:

        for (int i = 0; i < sizeOfList.length; i++)
       if (sizeOfList.length>1){ 
           double[] firstHalf = new double [sizeOfList.length/2];
           System.arraycopy(sizeOfList, 0, firstHalf, 0sizeOfList.length/2);
           mergeSort(firstHalf);

           int secondHalfLenght = sizeOfList.length - sizeOfList.length/2;
           double[] secondHalf = new double[secondHalfLenght];
           System.arraycopy(sizeOfList, sizeOfList.length/2, secondHalf, 0,secondHalfLenght);
           merge(firstHalf, secondHalf, sizeOfList);

       }   
}

    public  void merge(double[] list1, double[] list2, double[] average){
        int current1 = 0;
        int current2 = 0;
        int current3 = 0;
        while( current1 < list1.length &&  current2 < list2.length){
        if (list1[current1] <list2[current2])
            average[current3++] = list1[current1++];
        else
            average[current3++] = list2[current2++];
    }
        while (current1 < list1.length) 
            average[current3++] = list1[current2++];
        while (current2 < list2.length)
            average[current3++] = list2[current2++];

    }
    private void mergeSort( ){
        int [] sizeOfList;
        sizeOfList = students.size();
    mergeSort(sizeOfList);


                  // System.out.println(sizeOfList[i] + "");
 //the error ends here everything else is ok with the program
    }
 //this it to print to a file
  public void writeStudents(){
    JFileChooser fileChooser = new JFileChooser();
    if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
        java.io.File  file = fileChooser.getSelectedFile();

        try {
            output = new PrintWriter(file);
            String name = "Name";
            String grade1 = "Grade 1";
            String grade2 = "Grade 2";
            String grade3 = "Grade 3";
            String avg = "Average";
            String status = "Status";
                  output.printf("%15s %15s %15s %15s %15s %14s %n", name,    grade1,grade2, grade3, avg, status);
            for(int i = 0; i <= students.size() - 1; i++){
                output.printf("%15s %15.2f %15.2f %15.2f %15.2f %15s %n", 
                                    students.get(i).getName(),
                                    (double)students.get(i).getGrade1(),
                                    (double)students.get(i).getGrade2(),
                                    (double)students.get(i).getGrade3(),
                                    students.get(i).getAverage(),
                                    students.get(i).getStatus());
            }
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }               
        output.close();             
        }
    }        



    private void mergeSort(int sizeOfList) {
    throw new UnsupportedOperationException("Not supported yet."); 
}


}
  //this  is the main package and cannot be modify 
     package teststudent;

       public class TestStudent {

   public static void main(String[] args) {
   StudentList studentList = new StudentList();

   studentList.readStudents(); 

       studentList.writeStudents();    

         studentList.sortStudents(); 

       studentList.writeStudents();
}

  }

但是,当文件目录不存在时,这些解决方案都不会返回[]。为什么呢?

1 个答案:

答案 0 :(得分:1)

是的,这是可行的:

fun wasExceptionRaised f x = (f x; false) handle e => true

这将执行f x,丢弃结果并返回false,除非引发异常,在这种情况下,异常e将被处理,丢弃并true返回。虽然您可能希望处理特定的异常而不是其他异常,例如在测试时:

val test_should_not_find_file =
    (TextIO.openIn "non-existing-file"; false)
    handle Io { cause = SysErr ("No such file or directory", _), ... } => true
         | _ => false

如果您只是记录是否抛出了异常,您可以执行类似的操作:

structure YourLogger =
struct
    fun logException e =
        let val time = Date.toString (Date.fromTimeLocal (Time.now ()))
            val msg = "On " ^ time ^ " an error occurred: "
                    ^ General.exnName e ^ ".\n"
                    ^ General.exnMessage e
        in appendFile "myLog.txt" msg end

    fun logged f x = f x handle e => (logException e; raise e)
end

现在,您可以调用f x来获取相同的结果,而不是调用logged f x,而是记录任何异常。